Open In App

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. 

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.

pandas Series.dot() Method Implementations

Importing necessary libraries




# 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.




# 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




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




# 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()

pandas DataFrame.dot() Method Implementations

1. With Series Data

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




# 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:

2. With Dataframe




# 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:

3. With Numpy Array




# 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.


Article Tags :