2

How can I merge 2 dataframes df1 and df2 using a common column 'ADD' into df3? Both df1 and df2 have a common column 'ADD'.

I want to use df2 as a mapping table for covert ADD into a ST value.

I have tried to convert df2 into Series or Dictionary, but nether seems work.

 df1 = 

    Name    ADD
 1  A       12
 2  B       54
 3  C       34
 4  D       756
 5  E       43

 df2 = 

    ADD     ST
 1  12      CA
 2  54      CA
 3  34      TX


 df3 =

    Name    ADD     ST
 1  A       12      CA
 2  B       54      CA
 3  C       34      TX
 4  D       756     nan
 5  E       43      nan
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
notilas
  • 2,323
  • 4
  • 23
  • 36

1 Answers1

4

You have to do an outer merge (join):

In [11]: df1.merge(df2, how='outer')
Out[11]: 
  Name  ADD   ST
0    A   12   CA
1    B   54   CA
2    C   34   TX
3    D  756  NaN
4    E   43  NaN
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535