1

I have two dataframes

df_a

|id    |name|    email|
|0   | Jack  |  jack@gmail.com|
|1   | James |   james@gmail.com|
|2   | Alice |   alice@gmail.com|

df_b

|id    |name|    email|
|2   | Alice |  alice@live.com    |
|3   | Allen |   allen@gmail.com   |
|4   | Amy   | amy@gmail.com       |

I want to merge two dataframe base on id,and using df_a's email .get result like this:

df_result

|id    |name|    email|
|0    |Jack |   jack@gmail.com  |
|1    |James|    james@gmail.com|
|2    |Alice|    alice@gmail.com|
|3    |Allen|    allen@gmail.com|
|4    |Amy  |  amy@gmail.com    |

It is a little different like this question ,because id=2 Alice's email is different.

Masker
  • 76
  • 6

1 Answers1

1

You can use concat + drop_duplicates with specify columns for check dupes:

df = pd.concat([df_a, df_b]).drop_duplicates(['id','name'])
print (df)
   id   name            email
0   0   Jack   jack@gmail.com
1   1  James  james@gmail.com
2   2  Alice  alice@gmail.com
1   3  Allen  allen@gmail.com
2   4    Amy    amy@gmail.com
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252