1

I have 2 data frames - cities and stadiums. I want to merge them

cities
     Name   Stadium
0     c1      a
1     c2     NaN
2     c3     NaN
3     c4      b

stadiums
      Name     Capacity Type Col4 Col5
0       a        1       1    1     4
1       b        3       2    2     5
2       c        3       1    1     6

Output:
     Name   Stadium Capacity Type
0     c1      a       1       1
1     c2     NaN     NaN     NaN
2     c3     NaN     NaN     NaN
3     c4      b       3       2

When I merge them with one the following statements, I get additional rows:

cities = pd.merge(cities,stadiums[['Name','Capacity','Type']],left_on='Stadium',right_on='name',how='left')

cities = pd.merge(cities,stadiums[['Name','Capacity','Type']],left_on='Stadium',right_on='name',how='outer')

How do I merge these dataframes?

2 Answers 2

2

Your problem is that you're performing an outer merge (how='outer'), which means all the rows from both dataframes are included in the output. If you just want to match on the rows from the cities dataframe, try how='left' instead.

Sign up to request clarification or add additional context in comments.

Comments

0

outer to left

cities = pd.merge(cities,stadiums[['Name','Capacity','Type']],left_on='Stadium',right_on='Name',how='left')
cities
Out[157]: 
  Name_x Stadium Name_y  Capacity  Type
0     c1       a      a       1.0   1.0
1     c2     NaN    NaN       NaN   NaN
2     c3     NaN    NaN       NaN   NaN
3     c4       b      b       3.0   2.0

1 Comment

@Shankze you have duplicated in merge keys , try to remove them before merge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.