有两个数据帧:
df1:
id sen a1 a2 a3 a4
1 I am here I am here
2 You are there,Bar You are there Bar
3 take your time take your time
df2:
id sen b1 b2 b3
1 I am here
2 no way for this
我需要比较f1.sen和f2.sen,如果它们相等,请使用a1,a2,a3和a4列中的值填写b1,b2和b3列。 输出应该是这样的:
df2:
id sen b1 b2 b3
1 I am here I am here
2 no way for this
这是我的代码:
if df2['sen'].equals(df1['sen']):
df2['b1'] = df1['a1']
df2['b2'] = df1['a2']
df2['b3'] = df1['a3']
但它不起作用。有什么建议吗?
我也用过这个:
if df2.sen == df1.loc[df2.index].sen:
df2['B1'] = df1['A1']
df2['B2'] = df1['A2']
df2['B3'] = df1['A3']
但输出是:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). How I can fix it? any suggestion?
答案 0 :(得分:1)
你可以在这里做左联盟。
joined = df2.join(df1.set_index("sen"), how="left", on="sen")
然后将b1的值从a1分配给a3
joined.b1=joined.a1
joined.b2=joined.a2
joined.b3=joined.a3
删除a *列
for col in joined.columns:
if col[0] == "a":
del(joined[col])