I got two dataframes:
DF
Contact | phone1 | phone2 |
---|---|---|
1 | 061234 | |
2 | 0612345 |
DF1
Contact | phone1 | phone2 |
---|---|---|
1 | 061236 | 12344 |
2 | 1222 | 0612347 |
I am using the update() function to update the two dataframes
df.set_index('Contact', inplace=True)
df.update(df1.set_index('Contact'))
df.reset_index() # to recover the initial structure
Result
Contact | phone1 | phone2 |
---|---|---|
1 | 061236 | 12344 |
2 | 1222 | 0612347 |
Is it possible to get the other numbers too? Something like this
Contact | phone1 | phone2 | phone3 |
---|---|---|---|
1 | 061236 | 12344 | 061234 |
2 | 1222 | 0612347 | 0612345 |
CodePudding user response:
We can use the merge
method to do it :
df = pd.merge(df,
df1,
how='outer',
left_on=['Contact'],
right_on=['Contact'],
suffixes=['_df1',
'_df2'])
And then rename the columns as you wish using :
df.columns = ['contact', 'phone1', 'phone2', 'phone3']