Skip to content Skip to sidebar Skip to footer

Concatenate Two Dataframes Of Different Sizes (pandas)

I have two dataframes with unique ids. They share some columns but not all. I need to create a combined dataframe which will include rows from missing ids from the second dataframe

Solution 1:

In this case using combine_first

df1.set_index('id').combine_first(df2.set_index('id')).reset_index()
Out[766]: 
   idmetric1metric20a123.01.01b22.02.02c356.03.03d412.04.04f54.05.05g634.06.06h72.07.07j812.08.08k129.09.09l110.010.010m200.011.011q812.0NaN12w110.0NaN13z129.0NaN

Solution 2:

If you have many df to combine probably you'll find pd.concat quite usefull

pd.concat([df_1, df_2, ..., df_n])

Post a Comment for "Concatenate Two Dataframes Of Different Sizes (pandas)"