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

Expand dataframe by lookup in another dataframe

I have two dataframes:

df1
engine       id
F1 D2 J3     100234
F1 D2 J3     100238
K2 JM F2     200788
K2 JM F2     200790

df2
engine       id
F1 D2 J3     100234
F1 D2 J3     100244
K2 JM F2     200788
K2 JM F2     200850
K2 JM F2     200690

I would like to extend df1 with rows from df2 that are not found in df1 for that engine and id combination. Resulting df would not contain duplicates and would look something like this:

engine       id
F1 D2 J3     100234
F1 D2 J3     100238
K2 JM F2     200788
K2 JM F2     200790
F1 D2 J3     100244
K2 JM F2     200850
K2 JM F2     200690

How can I achieve this?

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 by specifying your two columns and with how="outer":

pd.merge(df1, df2, on=['engine', 'id'], how='outer')

Output:

     engine      id
0  F1 D2 J3  100234
1  F1 D2 J3  100238
2  K2 JM F2  200788
3  K2 JM F2  200790
4  F1 D2 J3  100244
5  K2 JM F2  200850
6  K2 JM F2  200690
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