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

相关推荐
左师佑图10 小时前
综合案例:Python 数据处理——从Excel文件到数据分析
开发语言·python·数据分析·excel·pandas
景早3 天前
pandas简介
pandas
懒惰蜗牛6 天前
Day10:Python实现Excel自动汇总
python·numpy·pandas·pip·1024程序员节·python读写excel
CodeCraft Studio7 天前
国产化Excel开发组件Spire.XLS教程:在Python中将Pandas DataFrame导出到Excel的详细教程
python·excel·pandas
sunbyte8 天前
从零掌握 Pandas:数据分析的黄金钥匙|01:认识Pandas
数据挖掘·数据分析·pandas
是梦终空9 天前
计算机毕业设计240—基于python+爬虫+html的微博舆情数据可视化系统(源代码+数据库)
爬虫·python·pandas·课程设计·毕业论文·计算机毕业设计·微博舆情可视化
万粉变现经纪人11 天前
如何解决 pip install -r requirements.txt 私有索引未设为 trusted-host 导致拒绝 问题
开发语言·python·scrapy·flask·beautifulsoup·pandas·pip
万粉变现经纪人12 天前
如何解决 pip install -r requirements.txt 私有仓库认证失败 401 Unauthorized 问题
开发语言·python·scrapy·flask·beautifulsoup·pandas·pip
m***记13 天前
Python 数据分析入门:Pandas vs NumPy 全方位对比
python·数据分析·pandas
小钱c713 天前
Python使用 pandas操作Excel文件并新增列数据
python·excel·pandas