14

I have two pandas df x and y, both with the same 3 columns A B C (not nullable). I need to create a new df z, obtained by "subtracting from x the rows which are entirely identical to the rows of y", i.e. a

x left join y on x.A=y.A and x.B=y.B and x.C=y.C
where y.A is null

How would I do that? Got stuck with indexes, concat, merge, join, ...

Example:

dataframe x
A    B    C
q1   q2   q3
q4   q2   q3
q7   q2   q9

dataframe y
A    B    C
q4   q2   q3

dataframe z
A    B    C
q1   q2   q3
q7   q2   q9
2
  • Can you create data sample and expected output? Commented Mar 26, 2018 at 8:42
  • Sure, example added Commented Mar 26, 2018 at 8:48

2 Answers 2

27

I think need merge with indicator and filter only rows from left DataFrame:

df = x.merge(y, indicator='i', how='outer').query('i == "left_only"').drop('i', axis=1)
print (df)
    A   B    C
0  q1  q2   q3
2  q7  q2  q93

In earlier versions of pandas, it may be necessary to replace .drop('i', axis=1) with .drop('i',1). The former is necessary to avoid warnings in later versions of Pandas.

Sign up to request clarification or add additional context in comments.

Comments

2

Here are a few other ways to remove certain lines from a dataframe using another dataframe:

pd.concat([dfx,dfy]).drop_duplicates(keep=False)

or

dfx.loc[[i not in dfy.to_records(index = False) for i in dfx.to_records(index = False)]]

or

dfx.loc[~dfx.apply(tuple,axis=1).isin(dfy.to_records(index = False))]

or

pd.MultiIndex.from_frame(dfx).symmetric_difference(pd.MultiIndex.from_frame(dfy)).to_frame().reset_index(drop=True)

pd.DataFrame(set(dfx.apply(tuple,axis=1)).symmetric_difference(dfy.apply(tuple,axis=1)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.