0

I have two pandas datasets:

    Name        A   B
1   Michael     1   2
2   Peter       3   4

and

    Name        C   D
1   Peter       8   9
2   John        5   6

How can I merge these datasets to get the following result:

    Name        A   B   C   D
1   Michael     1   2   -   -
2   Peter       3   4   8   9
3   John        -   -   5   6

The value for "-" should either be 0 or NaN.

Michael
  • 538
  • 1
  • 7
  • 16

1 Answers1

1

You can use pd.DataFrame.merge to perform an outer join:

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

#      Name    A    B    C    D
#0  Michael  1.0  2.0  NaN  NaN
#1    Peter  3.0  4.0  8.0  9.0
#2     John  NaN  NaN  5.0  6.0
Pablo C
  • 4,661
  • 2
  • 8
  • 24