pandas:合并(连接)多列上的两个数据框

新手上路,请多包涵

我正在尝试使用两列加入两个熊猫数据框:

 new_df = pd.merge(A_df, B_df,  how='left', left_on='[A_c1,c2]', right_on = '[B_c1,c2]')

但出现以下错误:

 pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4164)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4028)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13166)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13120)()

KeyError: '[B_1, c2]'

知道什么应该是正确的方法吗?谢谢!

原文由 Edamame 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 607
1 个回答

尝试这个

new_df = pd.merge(A_df, B_df,  how='left', left_on=['A_c1','c2'], right_on = ['B_c1','c2'])

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

left_on :标签或列表,或类似数组的字段名称,以加入左侧 DataFrame。可以是 DataFrame 长度的向量或向量列表,以使用特定向量作为连接键而不是列

right_on :标签或列表,或类似数组的字段名称,以加入右侧 DataFrame 或每个 left_on 文档的向量/向量列表

原文由 Shijo 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题