How to merge two DataFrame containing same keys but different values in same columns in python

Question:

I have one dataframe that contains all ids

df1 = pd.DataFrame({'id': ['A01', 'A02', 'A03', 'A04', 'A05', 'A06','A07'],
                        'Name': ['', '', '', '', 'MKI', 'OPU','']})

Second DataFrame that contains some Ids has different name in them

df2 = pd.DataFrame({'id': ['A01', 'A05', 'A06', 'A03'],
                    'Name': ['ABC', 'TUV', 'MNO', 'JKL']})

I want to merge both of them , where same Ids where one contains some name replace empty name and merge

Also DF2 name have to consider while merging

MERGE OUTPUT DF:-

df3 = {'id': ['A01', 'A02', 'A03', 'A04', 'A05', 'A06','A07'],
       'Name': ['ABC','', 'JKL','', 'TUV', 'MNO','']}

Note:- Merge two dataframe with same columns and some same id but different name, if it’s empty replace it other dataframe value ,Also get two value for same id then replace it with DF2
consider DF2 as MAIN , and want all data of Df1

Asked By: ZAVERI SIR

||

Answers:

Use left join by only df1['id'] column and replace missing values by empty strings:

df = df1[['id']].merge(df2, on='id', how='left').fillna({'Name':''})
print (df)
    id Name
0  A01  ABC
1  A02     
2  A03  JKL
3  A04     
4  A05  TUV
5  A06  MNO
6  A07     
Answered By: jezrael