Pandas DataFrame dot() Method

In this tutorial, we will learn the python pandas DataFrame.dot() method. It computes the matrix multiplication between the DataFrame and others. This method computes the matrix product between the DataFrame and the values of another Series, DataFrame or a numpy array. It returns a Series or DataFrame.

  • The dimensions of DataFrame and other must be compatible in order to compute the matrix multiplication.

  • In addition, the column names of DataFrame and the index of other must contain the same values, as they will be aligned prior to the multiplication.

The below shows the syntax of the DataFrame.dot() method.

Syntax

DataFrame.dot(other)

Parameters

Other: Series, DataFrame or array-like. The other object to compute the matrix product with.

Example 1: The DataFrame.dot() Method in Pandas

In the below example, two DataFrames are created and the elements in one DataFrame are multiplied with the elements in the other DataFrame. The DataFrame.dot() method returns the DataFrame by adding all multiplied values.

#importing pandas as pd
import pandas as pd
#creating DataFrames
df1=pd.DataFrame([[0,1,1, 2],[2,1,1,0]],columns=('A','B','C','D'))
df2=pd.DataFrame([[1, 2], [2, 3],[2, 3],[4,1]],index=('A','B','C','D'))
print(df1)
print(df2)
print(df1.dot(df2))

Once we run the program we will get the following output.


A B C D
0 0 1 1 2
1 2 1 1 0
0 1
A 1 2
B 2 3
C 2 3
D 4 1
0 1
0 12 8
1 6 10

Example 2: The DataFrame.dot() Method in Pandas

The below example is similar to the previous one. Create two DataFrames, apply DataFrame.dot() method and get the matrix multiplication DataFrame.

#importing pandas as pd
import pandas as pd
#creating DataFrames
df1= pd.DataFrame([[1, 1, 1],[2, 2, 2],[3, 3, 3]])
df2= pd.DataFrame([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
print(df1.dot(df2))

Once we run the program we will get the following output.


0 1 2
0 1 1 1
1 2 2 2
2 3 3 3

Example 3: Computing matrix multiplication using the DataFrame.dot() method with Series.

In the below example, one DataFrame and Series is created and the elements in one DataFrame are multiplied with the elements in the Series. The DataFrame.dot() method returns the DataFrame by adding all multiplied values.

#importing pandas as pd
import pandas as pd
#creating DataFrames
df1= pd.DataFrame([[1, 1, 1],[2, 2, 2],[3, 3, 3]],columns=('a','b','c'))
df2=pd.Series([1, 1, 2],index=('a','b','c'))
print(df1)
print(df2)
print(df1.dot(df2))

Once we run the program we will get the following output.


a b c
0 1 1 1
1 2 2 2
2 3 3 3
a 1
b 1
c 2
dtype: int64
0 4
1 8
2 12
dtype: int64

Example 4: ValueError While multiplying DataFrame

We will get the ValueError exception if the matrices are not aligned. The below example shows the same.

#importing pandas as pd
import pandas as pd
#creating DataFrames
df1=pd.DataFrame([[0, 1], [1, 2],[2, 0]])
df2=pd.DataFrame([[1, 2], [2, 3],[2, 3]])
print(df1.dot(df2))

Once we run the program we will get the following output.


ValueError: matrices are not aligned

Conclusion

In this tutorial, we learned the python pandas DataFrame.dot() method. We solved the example by multiplying the DataFrame with DataFrame, Series, np.array and got the matrix multiplication of DataFrame.