【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() 方法,可以高效实现线性代数中的矩阵乘法操作,适用于数据分析、机器学习等场景。

相关推荐
benchmark_cc1 天前
批量获取量化数据时,如何设置合理的超时和重试机制?——QuantDash 高性能实战指南
开发语言·人工智能·python·pandas·量化·quantdash
2601_966949651 天前
使用 Pandas 读取批量数据时如何避免内存溢出?QuantDash 量化数据工程师避坑指南
开发语言·python·pandas·tushare·akshare·quantdash
三十岁老牛再出发3 天前
08.18每日总结
c++·python·numpy·pandas
虎头金猫5 天前
如何在群晖NAS上通过Docker部署CloudSaver?群晖部署CloudSaver教程|聚合资源搜索并实现远程访问
运维·服务器·网络·python·docker·容器·pandas
Uncommon.7 天前
使用pandas处理csv并转为张量
pytorch·python·深度学习·pandas
STR_Liang9 天前
pandas.read_html 报错FileNotFoundError、OSError Traceback (most recent call last)
pandas
㳺三才人子10 天前
初探 Data Analysis - Matplotlib
python·plotly·pandas·matplotlib
quantdash_cc10 天前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
菜冻鱼11 天前
Python-sklearn-评估指标
开发语言·人工智能·python·机器学习·numpy·pandas·sklearn
菜冻鱼11 天前
Python-sklearn-模型选择
开发语言·人工智能·python·机器学习·numpy·pandas·sklearn