Search code examples
pandasdataframemergeconcatenationrow

How to combine two dataframes with different number of both rows and columns using pandas


I have two data frames with different number of both rows and columns. Some indices and columns are common.

df1

  A B C
a 1 0 0
b 2 3 4
c 4 5 6

df2

  A C D E
a 1 0 2 3
b 2 4 2 4
d 1 4 5 6
e 1 2 3 4

The result should be

  A  B  C  D  E
a 1  0  0  2  3
b 2  3  4  2  4
c 4  5  6  NA NA
d 1  NA 4  5  6
e 1  NA 2  3  4

I tried "join", "merge", and "concat" but it wasn't successful. Would you help me out?

Thanks.


Solution

  • You can use combine_first

    df = df1.combine_first(df2)
    

    Result

       A    B  C    D    E
    a  1  0.0  0  2.0  3.0
    b  2  3.0  4  2.0  4.0
    c  4  5.0  6  NaN  NaN
    d  1  NaN  4  5.0  6.0
    e  1  NaN  2  3.0  4.0