【Pandas】pandas DataFrame dot

Pandas2.2 DataFrame

Binary operator functions

方法 描述
DataFrame.add(other) 用于执行 DataFrame 与另一个对象(如 DataFrame、Series 或标量)的逐元素加法操作
DataFrame.add(other[, axis, level, fill_value]) 用于执行 DataFrame 与另一个对象(如 DataFrame、Series 或标量)的逐元素加法操作
DataFrame.sub(other[, axis, level, fill_value]) 用于执行逐元素的减法操作
DataFrame.mul(other[, axis, level, fill_value]) 用于执行逐元素的乘法操作
DataFrame.div(other[, axis, level, fill_value]) 用于执行逐元素的除法操作
DataFrame.truediv(other[, axis, level, ...]) 用于执行逐元素的真除法操作
DataFrame.floordiv(other[, axis, level, ...]) 用于执行逐元素的地板除法操作
DataFrame.mod(other[, axis, level, fill_value]) 用于执行逐元素的取模操作
DataFrame.pow(other[, axis, level, fill_value]) 用于对 DataFrame 中的元素进行幂运算
DataFrame.dot(other) 用于计算两个 DataFrame(或 DataFrame 与 Series/数组)之间的**矩阵点积(矩阵乘法)**的方法

pandas.DataFrame.dot()

pandas.DataFrame.dot(other) 是 Pandas 中用于计算两个 DataFrame(或 DataFrame 与 Series/数组)之间的**矩阵点积(矩阵乘法)**的方法。它的行为类似于线性代数中的矩阵乘法,结果的行索引与原始 DataFrame 的行索引对齐,列索引与 other 的列索引对齐。


语法
python 复制代码
DataFrame.dot(other)
  • 参数 other:可以是另一个 DataFrame、Series 或类数组结构(如 NumPy 数组)。
  • 返回值:一个新的 DataFrame 或 Series,具体取决于输入类型。

关键规则
  1. 维度对齐 :调用方的列数必须与 other 的行数相等。
  2. 索引对齐:Pandas 会根据行/列标签自动对齐数据。若标签不匹配,可能导致 NaN 或错误。
  3. * 的区别df.dot(other) 是矩阵乘法,而 df * other 是逐元素相乘。

示例
示例 1:DataFrame × DataFrame
python 复制代码
import pandas as pd

# 创建两个 DataFrame
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['row1', 'row2'])
df2 = pd.DataFrame({'C': [5, 6], 'D': [7, 8]}, index=['A', 'B'])

# 矩阵乘法:df1 的列索引(A, B)与 df2 的行索引(A, B)对齐
result = df1.dot(df2)
print(result)

输出

复制代码
       C   D
row1  23  31
row2  34  46

计算过程

  • row1 的结果:
    • C = 1*5 + 3*6 = 5 + 18 = 23
    • D = 1*7 + 3*8 = 7 + 24 = 31
  • row2 的结果:
    • C = 2*5 + 4*6 = 10 + 24 = 34
    • D = 2*7 + 4*8 = 14 + 32 = 46

示例 2:DataFrame × Series
python 复制代码
import pandas as pd

df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=['a', 'b', 'c'])
s = pd.Series([10, 20], index=['X', 'Y'])  # Series 的索引与 df 的列对齐

result = df.dot(s)
print(result)

输出

复制代码
a     90   # 1*10 + 4*20 = 10 + 80 = 90
b    120   # 2*10 + 5*20 = 20 + 100 = 120
c    150   # 3*10 + 6*20 = 30 + 120 = 150
dtype: int64

示例 3:DataFrame × 数组
python 复制代码
import pandas as pd
import numpy as np

df = pd.DataFrame({'M': [1, 2], 'N': [3, 4]})
arr = np.array([[5, 6], [7, 8]])  # 2x2 数组

result = df.dot(arr)
print(result)

输出

复制代码
    0   1
0  26  30
1  38  44

计算过程

  • 第 0 行:1*5 + 3*7 = 5 + 21 = 26(列 0),1*6 + 3*8 = 6 + 24 = 30(列 1)
  • 第 1 行:2*5 + 4*7 = 10 + 28 = 38(列 0),2*6 + 4*8 = 12 + 32 = 44(列 1)

注意事项
  1. 维度不匹配 :若列数 ≠ other 的行数,抛出 ValueError

  2. 索引对齐问题 :若标签不匹配,可能生成 NaN。可用 .values 忽略索引:

    python 复制代码
    df1.dot(df2.values)  # 使用纯数值计算,忽略索引
  3. @ 运算符等价df1 @ df2df1.dot(df2) 结果相同。

通过 dot() 方法,可以高效实现线性代数中的矩阵乘法操作,适用于数据分析、机器学习等场景。

相关推荐
老哥不老2 天前
Python调用SQLite及pandas相关API详解
python·sqlite·pandas
Hello world.Joey3 天前
数据挖掘入门-二手车交易价格预测
人工智能·python·数据挖掘·数据分析·conda·pandas
liuweidong08023 天前
【Pandas】pandas DataFrame cumprod
pandas
wxl7812275 天前
基于flask+pandas+csv的报表实现
python·flask·pandas
chaodaibing5 天前
pandas读取pymysql和解析excel的一系列问题(版本不匹配)
excel·pandas
aiweker7 天前
python数据分析(九):Pandas 分类数据(Categorical Data)处理
python·数据分析·pandas
TravelLight927 天前
Python pandas 向excel追加数据,不覆盖之前的数据
python·excel·pandas
liuweidong08028 天前
【Pandas】pandas DataFrame abs
pandas
liuweidong08029 天前
【Pandas】pandas DataFrame ewm
pandas
数据系的公考小白10 天前
2025五一杯数学建模C题代码分享
python·数学建模·pandas·五一杯