The dot()
method is used to compute the dot product between DataFrames
or Series
. DataFrame.dot()
from DataFrame
class is used to take the dot product of two DataFrames
or DataFrame
and Series
.
Note: A
dot()
works like amul()
function, but instead of returning multiplication separately, it returns the sum of multiplied values at each row or index.
# SignatureDataFrame.dot(other)
It takes the following argument value:
other
: Another object of DataFrame
or Series
or array-like to compute dot product.It returns either Series
or DataFrame
.
other
is Series
, it returns series
after simple matrix dot product.other
is DataFrame
, it returns DataFrame
after dot product.Let's discuss different scenarios with some coding examples. Here we have listed some commonly used cases:
DataFrames
DataFrame
and Series
In this code, we'll take two DataFrame
of the same dimensions and evaluate their dot product.
# importing pandas module as alias pdimport pandas as pd# Creating a DataFramedf1 = pd.DataFrame([[0, 1, -2, -1],[0, 1, None, 1],[1, 3, 1, -1],[1, 1, 6, 1]])# Creating a DataFramedf2 = pd.DataFrame([[5, 3, 6, 4],[11, 3, 4, 3],[4, 3, 8, 3],[5, None, 2, 8]])# Calculating dot productprint(df1.dot(df2))
DataFrame
df1
that contains four DataFrame
df2
that contains four observations.df1.dot(df2)
to evaluate dot product in return.In this code snippet, we'll create a pandas DataFrame
and Series
object to calculate their dot product.
# importing pandas as pdimport pandas as pd# Creating the dataframedf = pd.DataFrame([[5, 3, 6, 4],[11, None, 4, 3],[4, 3, 8, None],[5, 4, 2, 8]])# Creating a seriesseries = pd.Series([1, 1, 2, 1])# Print the dot product between DataFrame and Seriesprint(df.dot(series))
DataFrame
df
that contains four observations.Series
to get Pandas series object of four values.df.dot(series)
to get dot product between DataFrame
and series
.