dot() method is used to compute the dot product between
DataFrame class is used to take the dot product of two
dot()works like a
mul()function, but instead of returning multiplication separately, it returns the sum of multiplied values at each row or index.
# Signature DataFrame.dot(other)
It takes the following argument value:
other: Another object of
Seriesor array-like to compute dot product.
It returns either
Series, it returns
seriesafter simple matrix dot product.
DataFrame, it returns
DataFrameafter dot product.
Let's discuss different scenarios with some coding examples. Here we have listed some commonly used cases:
In this code, we'll take two
DataFrame of the same dimensions and evaluate their dot product.
# importing pandas module as alias pd import pandas as pd # Creating a DataFrame df1 = pd.DataFrame([[0, 1, -2, -1], [0, 1, None, 1], [1, 3, 1, -1], [1, 1, 6, 1]]) # Creating a DataFrame df2 = pd.DataFrame([[5, 3, 6, 4], [11, 3, 4, 3], [4, 3, 8, 3], [5, None, 2, 8]]) # Calculating dot product print(df1.dot(df2))
df1that contains four
df2that contains four observations.
df1.dot(df2)to evaluate dot product in return.
In this code snippet, we'll create a pandas
Series object to calculate their dot product.
# importing pandas as pd import pandas as pd # Creating the dataframe df = pd.DataFrame([[5, 3, 6, 4], [11, None, 4, 3], [4, 3, 8, None], [5, 4, 2, 8]]) # Creating a series series = pd.Series([1, 1, 2, 1]) # Print the dot product between DataFrame and Series print(df.dot(series))
dfthat contains four observations.
Seriesto get Pandas series object of four values.
df.dot(series)to get dot product between
View all Courses