-2

I have for example 2 data frames with user and their rating for each place such as:

Dataframe 1:

Name    Golden Gate
Adam    1
Susan   4
Mike    5
John    4

Dataframe 2:

Name    Botanical Garden
Jenny   1
Susan   4
Leslie  5
John    3

I want to combine them into a single data frame with the result:

Combined Dataframe:

Name    Golden Gate   Botanical Garden
Adam    1             NA
Susan   4             4
Mike    5             NA
John    4             3
Jenny   NA            1
Leslie  NA            5

How to do that?

Thank you.

Mel
  • 639
  • 2
  • 6
  • 15

2 Answers2

0

You need to perform an outer join or a concatenation along an axis:

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

Output:

     Name  Golden Gate  Botanical Garden
0    Adam          1.0               NaN
1   Susan          4.0               4.0
2    Mike          5.0               NaN
3    John          4.0               3.0
4   Jenny          NaN               1.0
5  Leslie          NaN               5.0
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
0

I found that pandas merge with how='outer' solves the problem. The link provided by @Celius Stingher is useful

Mel
  • 639
  • 2
  • 6
  • 15