Need to merge a DataFrame with another DataFrame without affecting existing data
df1:
| Name | Subject | mark |
|---|---|---|
| a | Ta | 52 |
| b | En | |
| c | Ma | |
| d | Ss | 60 |
df2:
| Name | mark |
|---|---|
| b | 57 |
| c | 58 |
Expected Output:
| Name | Subject | mark |
|---|---|---|
| a | Ta | 52 |
| b | En | 57 |
| c | Ma | 58 |
| d | Ss | 60 |
Try using merge and combine_first:
>>> df = df1.merge(df2, on='Name', how='outer')
>>> df['mark'] = df.pop('mark_x').combine_first(df.pop('mark_y'))
>>> df
Name Subject mark
0 a Ta 52.0
1 b En 57.0
2 c Ma 58.0
3 d Ss 60.0
>>>