pythonpython-3.xpandasdataframepandas-merge

Merge two DataFrames by combining duplicates and concatenating nonduplicates


I have two DataFrames:

df = pd.DataFrame({'A':[1,2],
             'B':[3,4]})
    A   B
0   1   3
1   2   4

df2 = pd.DataFrame({'A':[3,2,1],
             'C':[5,6,7]})


    A   C
0   3   5
1   2   6
2   1   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 3   NaN 5 
1 2   4   6
2 1   3   7

Solution

  • 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