Search code examples
pythonpandasdataframeouter-join

Remove rows that are in another dataframe


I have two Dataframes

df1:

A   B   C   D   E   F   G
1   2   3   4   5   6   7
8   9   0   1   2   3   4
5   6   7   8   9   0   1

df2:

A   B   C   D   E   F   G
5   6   7   8   9   0   1

how would I remove the rows that are in df2 so that:

output:

A   B   C   D   E   F   G
1   2   3   4   5   6   7
8   9   0   1   2   3   4

I have looked at other examples and most of them join based off one column, how do you do this with multiple columns?


Solution

  • Try merge

    out = df1.merge(df2,how='left',indicator=True).loc[lambda x : x['_merge']=='left_only']
    Out[128]: 
       A  B  C  D  E  F  G     _merge
    0  1  2  3  4  5  6  7  left_only
    1  8  9  0  1  2  3  4  left_only