Home > Net >  How can I create a loop to merge two DataFrames?
How can I create a loop to merge two DataFrames?

Time:11-19

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

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?

CodePudding user response:

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
  • Related