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

Pandas – Merge smaller dataframe onto a larger one

I’ve got 2 data frames like this,

df1:

columnA  columnB columnC
data1    2       d       
data1    2       d
data1    2       d
data2    3       r
data2    3       r
data3    4       g
data3    4       g

df2:

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

columnA   columnD  columnE
data1     a        x
data1     a        x    
data1     a        x
data2     b        y
data2     b        y

After merging I want the output like below,

Expected output:

columnA  columnB columnC columnD columnE
data1    2       d       a       x
data1    2       d       a       x
data1    2       d       a       x
data2    3       r       b       y
data2    3       r       b       y
data3    4       g
data3    4       g

Is there a way to do this?

I’ve tried,
combined = pd.merge(df1, df2, how="inner", on="columnA") but this results in more rows than originally.

>Solution :

You first need to remove the duplicates from your second dataframe:

df2_nodup = df2.drop_duplicates()

Now you can use pd.merge() to create the desired output:

>>> pd.merge(df1, df2_nodup, how='left')
  columnA  columnB columnC columnD columnE
0   data1        2       d       a       x
1   data1        2       d       a       x
2   data1        2       d       a       x
3   data2        3       r       b       y
4   data2        3       r       b       y
5   data3        4       g     NaN     NaN
6   data3        4       g     NaN     NaN
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