pandas.DataFrame.dot#

DataFrame.dot(other)[源代码]#

计算 DataFrame 和 other 之间的矩阵乘积。

此方法计算 DataFrame 与另一个 Series、DataFrame 或 numpy 数组的矩阵乘积。

也可以使用 self @ other 调用。

Parameters:
otherSeries、DataFrame 或类数组对象

要计算矩阵乘积的另一个对象。

Returns:
Series 或 DataFrame

如果 other 是 Series,则返回 self 和 other 之间的矩阵乘积(作为 Series)。如果 other 是 DataFrame 或 numpy.array,则返回 self 和 other 的矩阵乘积(作为 DataFrame 或 np.array)。

参见

Series.dot

Series 的类似方法。

Notes

DataFrame 和 other 的维度必须兼容才能计算矩阵乘法。此外,DataFrame 的列名和 other 的索引必须包含相同的值,因为它们将在乘法之前对齐。

Series 的 dot 方法计算点积,而不是这里的矩阵乘积。

Examples

这里我们将一个 DataFrame 与一个 Series 相乘。

>>> df = pd.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]])
>>> s = pd.Series([1, 1, 2, 1])
>>> df.dot(s)
0    -4
1     5
dtype: int64

这里我们将一个 DataFrame 与另一个 DataFrame 相乘。

>>> other = pd.DataFrame([[0, 1], [1, 2], [-1, -1], [2, 0]])
>>> df.dot(other)
    0   1
0   1   4
1   2   2

请注意,dot 方法与 @ 得到相同的结果。

>>> df @ other
    0   1
0   1   4
1   2   2

当 other 是 np.array 时,dot 方法也有效。

>>> arr = np.array([[0, 1], [1, 2], [-1, -1], [2, 0]])
>>> df.dot(arr)
    0   1
0   1   4
1   2   2

注意对象的顺序改变不会影响结果。

>>> s2 = s.reindex([1, 0, 2, 3])
>>> df.dot(s2)
0    -4
1     5
dtype: int64