Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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     
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading