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

Join 2 dataframes in pandas using join

I have two data frames df1 and df2

df1

id     name
ada1  mike
ad23  tom
cev2  tim

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

 id   month.   sales
 ada1. 1/11.    23
 ada1. 4/11.    34
 ad23. 3/12.    34
 cev2. 4/11.    32

I need :

 id   month.   sales name
 ada1. 1/11.    23.  mike
 ada1. 4/11.    34.  mike
 ad23. 3/12.    34.  tom
 cev2. 4/11.    32.  tim

I am struck between left join or right join, what should i use.

>Solution :

Use pd.merge:

>>> df2.merge(df1, on='id', how='left')

     id month  sales  name
0  ada1  1/11     23  mike
1  ada1  4/11     34  mike
2  ad23  3/12     34   tom
3  cev2  4/11     32   tim
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