Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge pandas dataframes with same columns and varying unique IDs

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading