Merge pandas dataframes with same columns and varying unique IDs

Question:

I have these two dataframes:

df1 =
ID a b c d pred_1
1  ...       0
3  .         1
4  .         1
5            0
8            0 

df2 =
ID a b c d pred_2
2  ...       1
3  .         1
6  .         0
5            0
7            1 
9            1

So some of the IDs occur in both dataframes and all column names are the same except the prediction column. And I would like to merge them like this:

df3 =
ID a b c d pred_1 pred_2
1  ...       0      0
2  .         0      1
3  .         1      1
4            1      0     
5            0      0
6            0      0
7            0      1
8            0      0
9            0      1

I don’t know how to put it in prettier words. But can anybody help me please?

Asked By: John

||

Answers:

You can use pd.merge(..., how='outer')

pd.merge(df1, df2, how='outer').fillna(0)

Answered By: T C Molenaar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.