【发布时间】:2023-01-25 10:30:29
【问题描述】:
我有 2 个数据框,其示例如下:
df1:
Table Field
0 AOI AEDAT
1 AEI AEDTZ
2 AOI AEENR
3 AEO AENAM
4 AEO AEOST
df2:
View Field
0 Accounting 1 AEDAT
1 Accounting 1 AEDAT
2 Accounting 1 AEOST
3 Accounting 1 AEOST
我想要的是比较 2 个数据帧的 Field 列,如果它们相似,则在第三个数据帧中添加 df2 中的 View 字段,否则将 NA 作为行添加到第三个数据帧。
这是我到目前为止写的:
df3 = pd.DataFrame(columns=['view'])
for index, row in df1.iterrows():
for index2, row2 in df2.iterrows():
if row['Field'] == row2['Field']:
df3['view'].append(row2['View'])
当我运行这段代码时,出现以下错误:TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid
我该如何纠正这个问题?
【问题讨论】:
-
这回答了你的问题了吗? Pandas Merging 101