1
votes

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
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)

The doc speaks of column nameS for left_on and righ_on, so I assume it is possible? How? http://pandas.pydata.org/pandas-docs/dev/merging.html

1
are columns in B mutually exclusive? if so, you can create a third column and use that to merge - mkln
Yes, they are! You mean, merge B1 and B2 first? - MJP
yes B[1][pd.isnull(B[1])] = B[2][pd.isnull(B[1])] - mkln
Another way to combine: b['1'].combine_first(b['2']) - Zelazny7
@Zelazny7: that's better than the max hack. - DSM

1 Answers

0
votes

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]