Open In App

Python | Pandas Dataframe/Series.dot()

Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. 

Pandas Series.dot()

The dot() method is used to compute the dot product between DataFrames or Series.

Pandas Series.dot() is used to compute the dot product between DataFrames or Series. It works similarly to pd.mul() method, but instead of returning multiplied separate values, the dot product is returned (the sum of the multiplication of values at each index).

Syntax:

Syntax: Series.dot(other)

Parameters:

  • other: Other Series to be used to calculate DOT product

Return of the Series.dot() function

The return of the dot function can be a scalar, series, or numpy.ndarray.

  • The dot product of the Series and other if other is a Series.
  • The Series of the dot product of Series and each rows of other if other is a DataFrame .
  • A numpy.ndarray between the Series and each columns of the numpy array.

pandas Series.dot() Method Implementations

Importing necessary libraries

Python3




# importing pandas module
import pandas as pd
     
# importing numpy module
import numpy as np


1. With Series Data

In this example, two series are created from Python lists using Pandas Series() method. The method is then called on series1 and series2 is passed as a parameter. The result is then stored in a variable and displayed.

Python3




# creating series 1
series1 = pd.Series([7, 5, 6, 4, 9])
     
# creating series 2
series2 = pd.Series([1, 2, 3, 10, 2])
 
# storing in new variable
# calling .dot() method
ans = series1.dot(series2)
 
# display
print('Dot product = {}'.format(ans))


Output:

Dot product = 93

Explanation:

The elements in the caller series are multiplied by the element at the same index in the past series. All the multiplied values are then added to get the dot product. As in the above example, the series are:

[7, 5, 6, 4, 9] [1, 2, 3, 10, 2]

Dot product = 7*1 + 5*2 + 6*3 + 4*10 + 9*2 = 7 + 10 + 18 + 40 + 18 = 93

2. With Dataframe

Python3




series = pd.Series([5, 3, 7, 4], name='Series')
 
# Creating a DataFrame df1
df1 = pd.DataFrame([[0, 1, -9, 1],
                    [9, 1, 0, 1],
                    [1, 3, 1, -1],
                    [1, 1, 8, 1]])
 
# Calculating dot product with each row in the DataFrame
result_series = series.dot(df1)
 
# Printing the resulting Series
print(result_series)


Output:

0    38
1    33
2    -6
3     5
Name: Series, dtype: int64

Explanation:

The resulting Series will have values obtained by taking the dot product of the Series [5, 3, 7, 4] with each column in the DataFrame df1.

  1. For the first row of df1: 0⋅5+1⋅3+(−9)⋅7+1⋅4=38
  2. For the second row of df1: 9â‹…5+1â‹…3+0â‹…7+1â‹…4=33
  3. For the third row of df1: 1⋅5+3⋅3+1⋅7+(−1)⋅4=−6
  4. For the fourth row of df1: 1â‹…5+1â‹…3+8â‹…7+1â‹…4=5

Note: You do not need to take the transpose in this case. The dot product operation between a Series and a DataFrame in pandas is designed to handle this situation without the need for transposing.

3. With Numpy Array

Python3




# Given NumPy array or array-like
numpy_array = np.array([1, 2, 3, 4])
 
# Creating a pandas Series
series = pd.Series([5, 3, 7, 4], name='Series')
 
# Performing element-wise multiplication
result_series = series * numpy_array
 
# Printing the resulting Series
print(result_series)


Output:

0     5
1     6
2    21
3    16
Name: Series, dtype: int64

Output is a pandas Series where each element is the product of the corresponding elements in the original Series and NumPy array, i.e [5 * 1, 3 * 2, 7 * 3, 4 * 4], resulting in [5, 6, 21, 16].

Note: If there is any Null value in any of the series, the net result is NaN. NaN values should be removed/replaced using dropna() or fillna() methods respectively. 

Pandas Dataframe.dot() Method Implementations

We can also use Pandas Dataframe for using the dot() method. The dot() method multiplies each value from one DataFrame with the values from another DataFrame, and adds them together.

Syntax:

Syntax: dataframe.dot(other)

Parameters:

  • other: Other Dataframe to be used to calculate DOT product

Return of Dataframe.dot()

  • If other is a Series: It returns a Series representing the dot product of the DataFrame and the Series.
  • If other is a DataFrame: It returns a Series representing the dot product of the DataFrame and each row of the other DataFrame.
  • If other is a NumPy array or array-like: It returns a Series representing the dot product between the DataFrame and each column of the NumPy array.

pandas DataFrame.dot() Method Implementations

1. With Series Data

All the multiplied values are then added to get the dot product. 

Python3




# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})
 
# Create a Series
series = pd.Series([0.1, 0.2, 0.3], index=['A', 'B', 'C'])
 
# Dot product with a Series
result_series = df.dot(series)
print("Dot product with Series:")
print(result_series)


Output:

Dot product with Series:
0    3.0
1    3.6
2    4.2
dtype: float64

Explanation:

  • Dot product for row 0: 1×0.1+4×0.2+7×0.3=3.01×0.1+4×0.2+7×0.3=3.0
  • Dot product for row 1: 2×0.1+5×0.2+8×0.3=3.62×0.1+5×0.2+8×0.3=3.6
  • Dot product for row 2: 3×0.1+6×0.2+9×0.3=4.23×0.1+6×0.2+9×0.3=4.2

2. With Dataframe

Python3




# Creating a DataFrame df1
df1 = pd.DataFrame([[0, 1, -9, 1],
                    [9, 1, 0, 1],
                    [1, 3, 1, -1],
                    [1, 1, 8, 1]])
 
# Creating another DataFrame df2
df2 = pd.DataFrame([[5, 3, 7, 4],
                    [1, 3, 4, 3],
                    [4, 3, 8, 6],
                    [5, 8, 2, 8]])
 
 
# Calculating dot product
result = df1.dot(df2)
 
# Printing the result
print(result)


Output:

    0   1   2   3
0 -30 -16 -66 -43
1  51  38  69  47
2   7   7  25  11
3  43  38  77  63

Explanation:

  • Each row in the resulting DataFrame represents the dot product of the corresponding row in df1 and the columns in df2.
  • For example, the first row (row 0) is calculated as follows:
    • 0×5+1×3+(−9)×7+1×4=−300×5+1×3+(−9)×7+1×4=−30
    • 0×1+1×3+(−9)×3+1×8=−160×1+1×3+(−9)×3+1×8=−16
    • 0×4+1×3+(−9)×8+1×2=−660×4+1×3+(−9)×8+1×2=−66
    • 0×5+1×8+(−9)×2+1×8=−430×5+1×8+(−9)×2+1×8=−43
  • The same logic is applied to calculate each element in the resulting DataFrame.

3. With Numpy Array

Python3




# Creating a DataFrame df1
df1 = pd.DataFrame([[0, 1, -9, 1],
                    [9, 1, 0, 1],
                    [1, 3, 1, -1],
                    [1, 1, 8, 1]])
 
# Creating a NumPy array
numpy_array = np.array([5, 3, 7, 4])
 
# Calculating dot product with NumPy array
result = df1.dot(numpy_array)
 
# Printing the result
print(result)


Output:

0   -56
1    52
2    17
3    68
dtype: int64

Explanation:

Each row represents the dot product of the corresponding row in the original DataFrame df1 with the NumPy array numpy_array.

  • For the first row: 0 * 5 + 1 * 3 + (-9) * 7 + 1 * 4 = -56
  • For the second row: 9 * 5 + 1 * 3 + 0 * 7 + 1 * 4 = 52
  • For the third row: 1 * 5 + 3 * 3 + 1 * 7 + (-1) * 4 = 17
  • For the fourth row: 1 * 5 + 1 * 3 + 8 * 7 + 1 * 4 = 68



Last Updated : 31 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads