Home > Mobile >  Merge two DataFrames by combining duplicates and concatenating nonduplicates
Merge two DataFrames by combining duplicates and concatenating nonduplicates

Time:03-19

I have two DataFrames:

df:

    A   B
0   1   3
1   2   4

df2:

    A   C
0   1   5
1   2   6
2   3   7

and I want to merge in a way that the column 'A' add the different values between DataFrames but merge the duplicates.

Desired output:

    A   B   C
0   1   3   5
1   2   4   6
2   3   nan 7

CodePudding user response:

You can use combine_first:

df2 = df2.combine_first(df)

Output:

   A    B  C
0  1  3.0  5
1  2  4.0  6
2  3  NaN  7
  • Related