【发布时间】:2020-01-26 01:01:36
【问题描述】:
我正在尝试采用 pandas DataFrame 和 Series 的点积。但是,即使我将 pandas DataFrame 限制为与 Series 具有相同的列数,我仍然得到
ValueError:矩阵未对齐。
import pandas as pd
df1 = pd.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]])
df2 = pd.DataFrame([[0, 1, -2, -1, 1], [1, 1, 1, 1, 1]])
s = pd.Series([1, 1, 2, 1])
print(df1.shape) # (2,4)
print(df2.iloc[:, 1:len(df2.columns)].shape) #(2,4) also
df1.dot(s) # works fine
df2.iloc[:, 1:len(df2.columns)].dot(s) # throws ValueError
为什么会这样?
【问题讨论】:
-
df、df1或df2是指哪个变量? -
对不起,df2。刚刚编辑过。