Expand dataframe by lookup in another dataframe

Question:

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?

Asked By: detrraxic

||

Answers:

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
Answered By: Tranbi

You can do an outer join

out = df1.join(df2, on=['engine', 'id'], how='outer')
Answered By: oskros
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.