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 can I create a loop to merge two dataframes?

I everyone. Hope you enjoying your day.

I have two dataframes:

name age weight sex d_type
john 21 56 M futboll
martha 25 43 F soccer
esthela 29 53 F judo
harry 18 72 M karate
irving 24 61 M karate
jerry 21 56 M soccer
john_2 26 69 M futboll
malina 22 53 F soccer

And

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

d_type impact founds_in
futboll high federal
soccer medium state
judo medium federal
karate high federal

At the end i want a DF like this.

name age weight sex d_type impact founds_in
john 21 56 M futboll high federal
martha 25 43 F soccer medium state
esthela 29 53 F judo medium federal
harry 18 72 M karate high federal
irving 24 61 M karate high federal
jerry 21 56 M soccer medium state
john_2 26 69 M futboll high federal
malina 22 53 F soccer medium state

how can i do this in pandas? i need a loop or it’s better try in linux?

>Solution :

In Python

df1 = pd.DataFrame({'name': ['john', 'martha'],
                    'age': [21, 25],
                    'd_type': ['futbol', 'soccer']})
df2 = pd.DataFrame({'d_type': ['futbol', 'soccer'],
                    'impact': ['high', 'medium'],
                    'founds_in': ['federal', 'state']})
df1.merge(df2, on = 'd_type').set_index('name')

which gives:

name age d_type impact founds_in
john 21 futbol high federal
martha 25 soccer medium state
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