【发布时间】:2020-08-29 17:15:40
【问题描述】:
我有三个数据框。这是我的代码,我收到值错误。raise ValueError("Can only compare identically-labeled Series objects")
ValueError: 只能比较标签相同的 Series 对象。
这是我的代码:
df1
df2
df3
for i in df1: # for i from df1
x = df2.loc[df2['col_1'] == i, 'col_2'] #looking for i in col_1 of df2 and getting coresponding value of col_2 as x.
y = df3.loc[df3['col_1'] == x, 'col_2'] #looking for x in col_1 of df3 and getting coresponding value of col_2 as y
for 循环中的第一条语句运行正确,但在第二条语句中出现值错误。
【问题讨论】:
-
当你运行
for i in df1时,i只取df1的列名。我不认为那是你想要的。你在迭代df1的行吗? -
df1 只有一列。当我评论“for”循环的第二行时,它运行良好。
-
因此您可能在
x中有多个匹配项,而x不是单个值。你可以试试这个x = df2.loc[df2['col_1'] == i, 'col_2'].iloc[0]。 -
你试过
merge吗? -
x 中没有多个匹配项...及以上也不起作用。