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]:
id metric1 metric2
0 a 123.0 1.0
1 b 22.0 2.0
2 c 356.0 3.0
3 d 412.0 4.0
4 f 54.0 5.0
5 g 634.0 6.0
6 h 72.0 7.0
7 j 812.0 8.0
8 k 129.0 9.0
9 l 110.0 10.0
10 m 200.0 11.0
11 q 812.0 NaN
12 w 110.0 NaN
13 z 129.0 NaN
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)"