- API reference
- DataFrame
- pandas.DataFrame.dot
pandas.DataFrame.dot#
- DataFrame.dot(other)[source]#
Compute the matrix multiplication between the DataFrame and other.
This method computes the matrix product between the DataFrame and thevalues of an other Series, DataFrame or a numpy array.
It can also be called using
self@other
.- Parameters:
- otherSeries, DataFrame or array-like
The other object to compute the matrix product with.
- Returns:
- Series or DataFrame
If other is a Series, return the matrix product between self andother as a Series. If other is a DataFrame or a numpy.array, returnthe matrix product of self and other in a DataFrame of a np.array.
See also
Series.dot
Similar method for Series.
Notes
The dimensions of DataFrame and other must be compatible in order tocompute the matrix multiplication. In addition, the column names ofDataFrame and the index of other must contain the same values, as theywill be aligned prior to the multiplication.
The dot method for Series computes the inner product, instead of thematrix product here.
Examples
Here we multiply a DataFrame with a Series.
>>>df=pd.DataFrame([[0,1,-2,-1],[1,1,1,1]])>>>s=pd.Series([1,1,2,1])>>>df.dot(s)0 -41 5dtype: int64
Here we multiply a DataFrame with another DataFrame.
>>>other=pd.DataFrame([[0,1],[1,2],[-1,-1],[2,0]])>>>df.dot(other) 0 10 1 41 2 2
Note that the dot method give the same result as @
>>>df@other 0 10 1 41 2 2
The dot method works also if other is an np.array.
>>>arr=np.array([[0,1],[1,2],[-1,-1],[2,0]])>>>df.dot(arr) 0 10 1 41 2 2
Note how shuffling of the objects does not change the result.
>>>s2=s.reindex([1,0,2,3])>>>df.dot(s2)0 -41 5dtype: int64