大 pandas :在多列上合并(合并)两个数据框 [英] pandas: merge (join) two data frames on multiple columns

查看:769
本文介绍了大 pandas :在多列上合并(合并)两个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to join two pandas data frames using two columns:

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

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

Any idea what should be the right way to do this? Thanks!

推荐答案

尝试一下

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

http://pandas. pydata.org/pandas-docs/version/0.19.1/generated/pandas.DataFrame.merge.html

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

left_on : label or list, or array-like Field names to join on in left DataFrame. Can be a vector or list of vectors of the length of the DataFrame to use a particular vector as the join key instead of columns

right_on:要加入的标签或列表,或类似数组的字段名称 在右边的DataFrame或每个left_on docs的向量/向量列表中

right_on : label or list, or array-like Field names to join on in right DataFrame or vector/list of vectors per left_on docs

这篇关于大 pandas :在多列上合并(合并)两个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆