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

相关推荐
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
njxiejing1 天前
Pandas数据结构(DataFrame,字典赋值)
数据结构·人工智能·pandas
Calihen的学习日志2 天前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas
Source.Liu3 天前
【Python自动化】 21.2 Pandas 读取 Excel 时的 dtype 参数完全指南
python·自动化·pandas
Source.Liu3 天前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas
Source.Liu3 天前
【Python自动化】 21.1 Pandas 读取 Excel 文件的完整指南
python·自动化·pandas
偷心伊普西隆5 天前
Pandas DataFrame 指南
python·数据分析·pandas
chad__chang11 天前
Pandas的数据结构
数据结构·pandas
老歌老听老掉牙13 天前
Pandas DataFrame 列数操作完全指南
python·pandas
万粉变现经纪人13 天前
如何解决pip安装报错ModuleNotFoundError: No module named ‘websockets’问题
ide·pycharm·beautifulsoup·pandas·fastapi·pip·httpx