pandas.DataFrame.dot#

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

计算DataFrame和其他元素之间的矩阵乘法。

此方法计算DataFrame和其他Series、DataFrame或Numy数组的值之间的矩阵乘积。

也可以使用以下命令调用它 self @ other 在Python中>=3.5。

参数
other系列、DataFrame或类似数组

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

退货
系列或DataFrame

如果Other是级数,则将自身和Other之间的矩阵乘积作为级数返回。如果Other是DataFrame或numpy.array,则在np.array的DataFrame中返回self和Other的矩阵乘积。

参见

Series.dot

对级数采用类似的方法。

注意事项

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

级数的点法计算内积,而不是这里的矩阵乘积。

示例

在这里,我们将一个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

请注意,点方法给出的结果与@相同

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

如果Other是np.数组,则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