Merge two dataframes together in Pandas

Pandas

Import modules

import pandas as pd

 

Create some dummy data

raw_data1 = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],
'age': [20, 19, 22, 21],
'favorite_color': ['blue', 'blue', 'yellow', "green"],
'test_one': [88, 92, 95, 70]}
df1 = pd.DataFrame(raw_data1)
df1
age favorite_color name test_one
0 20 blue Willard Morris 88
1 19 blue Al Jennings 92
2 22 yellow Omar Mullins 95
3 21 green Spencer McDaniel 70
raw_data2 = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],
'test_two': [87, 94, 97, 72],
'test_three': [78, 100, 90, 85]}
df2 = pd.DataFrame(raw_data2)
df2
name test_three test_two
0 Willard Morris 78 87
1 Al Jennings 100 94
2 Omar Mullins 90 97
3 Spencer McDaniel 85 72

 

Merge the two dataframes, using 'name' as the join key

df3 = df1.merge(df2, how='left', on="name")
df3.head()
age favorite_color name test_one test_three test_two
0 20 blue Willard Morris 88 78 87
1 19 blue Al Jennings 92 100 94
2 22 yellow Omar Mullins 95 90 97
3 21 green Spencer McDaniel 70 85 72