Niam45
Niam45

Reputation: 578

How can I merge these dataframes of different lengths?

I have two dataframes. The first dataframe looks like:

Name First_Score
tom  10
nick 15
juli 14
Luke 30

The second:

Name Second_Score
nick 7
tom  5
juli  1

I want to join these two dataframes so that I have

Name First_Score Second_Score
nick 10          7
tom  15          5
juli 14          1
Luke 30          NA

How can I do this?

data = [['tom', 10], ['nick', 15], ['juli', 14], ['Luke', 30]]


df1 = pd.DataFrame(data, columns = ['Name', 'First_Score'])

data2 = [['nick', 7], ['tom', 5], ['Ali', 1]]

df2 = pd.DataFrame(data2, columns = ['Name', 'Second_Score'])

Upvotes: 1

Views: 38

Answers (1)

Sbunzini
Sbunzini

Reputation: 542

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

Upvotes: 3

Related Questions