简体   繁体   English

是否可以为熊猫(python)中的合并指定多个列?

[英]Is it possible to specify more than one column for the merge in pandas (python)?

I want to merge two tables, A and B. Table A has values in one column, in B these are spread amongst two columns: 我想合并两个表A和B。表A在一列中有值,在B中这些值分布在两列中:

A
1   2   3
X   X   HH
Y   D   HD


B
1   2
HD  NaN
Nan HH

I tried with no success: 我尝试没有成功:

newTable = pd.merge(A, B, how='left', left_on = 3, right_on = 1, 2) newTable = pd.merge(A,B,how ='left',left_on = 3,right_on = 1,2)

The doc speaks of column nameS for left_on and righ_on, so I assume it is possible? 该文档谈到了left_on和righ_on的列名S,所以我认为这是可能的吗? How? 怎么样? http://pandas.pydata.org/pandas-docs/dev/merging.html http://pandas.pydata.org/pandas-docs/dev/merging.html

When you specify columns to join on, make sure to refer to them as strings. 指定要连接的列时,请确保将其称为字符串。 If you need to refer to two columns in your join, use a list of strings. 如果需要在联接中引用两列,请使用字符串列表。

left_on = '3'
right_on = ['1', '2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM