大熊猫:在多列上合并(合并)两个数据框

发布于 2021-01-29 15:00:08

我正在尝试使用两列来连接两个熊猫数据框:

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]'

任何想法应该是正确的方法吗?谢谢!

关注者
0
被浏览
102
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    尝试这个

    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:标签或列表,或类似数组的字段名称,可在right DataFrame或每个left_on文档的向量/向量列表中加入



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看