pandas 与DataFrames进行点积时,矩阵未对齐

1wnzp6jl  于 2023-09-29  发布在  其他
关注(0)|答案(2)|浏览(80)
butter_prices
Almond butter   Peanut butter   Cashew butter
Price   10  8   12
weekly_sales
Almonds butter  Peanut butter   Cashew butter
Mon 12  15  0
Tues    3   3   7
Wed 9   19  18
Thurs   4   6   12
Fri 1   6   7

现在,在做

butter_prices.shape, weekly_sales.shape
((1, 3), (5, 3))

现在计算时显示错误

butter_prices.dot(weekly_sales.T)

ValueError: matrices are not aligned
应为2的点积

f0brbegy

f0brbegy1#

通过values

out = weekly_sales.dot(butter_prices.iloc[0].values)
out
Mon      240
Tues     138
Wed      458
Thurs    232
Fri      142
dtype: int64

或对齐名称

butter_prices.columns = weekly_sales.columns
butter_prices.dot(weekly_sales.T)
       Mon  Tues  Wed  Thurs  Fri
Price  240   138  458    232  142
vof42yt1

vof42yt12#

您在列名称Almonds butter-> Almond butter中有拼写错误:

weekly_sales = weekly_sales.rename(columns={"Almonds butter": "Almond butter"})
print(butter_prices.dot(weekly_sales.T))

图纸:

Mon  Tues  Wed  Thurs  Fri
Price  240   138  458    232  142

相关问题