The dot
method in pandas takes the dot product of a dataframe with another dataframe, series, or array-like object. The dot product is also known as matrix multiplication.
The shapes of both objects should be compatible with each other to calculate the dot product.
The illustration below shows how the dot
product is computed:
The syntax of the dot
method is as follows:
Dataframe.dot(other)
The dot
method has a single parameter: other
.
Other
refers to a dataframe, series, or array-like object with which the dot product is taken.
The dot
method returns a series if the other
parameter is a series object.
It returns a dataframe if the other
parameter is a dataframe.
The code snippet below shows how we can use the dot
operator in pandas:
import pandas as pdimport numpy as np# Dataframe with a seriesdf = pd.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]])s = pd.Series([1, 1, 2, 1])print("Dataframe with a series")print(df.dot(s))print('\n')# Dataframe with another Dataframeother = pd.DataFrame([[0, 1], [1, 2], [-1, -1], [2, 0]])print("Dataframe with another Dataframe")print(df.dot(other))print('\n')# Dataframe with an arrayarr = np.array([[0, 1], [1, 2], [-1, -1], [2, 0]])print("Dataframe with another array")print(df.dot(arr))print('\n')
The
dot
method is the same as the@
operator in pandas.
Free Resources