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. 

Python | Pandas Dataframe/Series.dot()

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. 

Similar Reads

Pandas Series.dot()

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

pandas Series.dot() Method Implementations

Importing necessary libraries...

Pandas Dataframe.dot() Method Implementations

...

pandas DataFrame.dot() Method Implementations

...

Contact Us