close

How to remove rows in a Pandas dataframe if the same row exists in another dataframe?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to remove rows in a Pandas dataframe if the same row exists in another dataframe in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to remove rows in a Pandas dataframe if the same row exists in another dataframe?

  1. How to remove rows in a Pandas dataframe if the same row exists in another dataframe?

    You could convert a and b into Indexs, then use the Index.isin method to determine which rows are shared in common:

  2. remove rows in a Pandas dataframe if the same row exists in another dataframe

    You could convert a and b into Indexs, then use the Index.isin method to determine which rows are shared in common:

Method 1

You an use merge with parameter indicator and outer join, query for filtering and then remove helper column with drop:

DataFrames are joined on all columns, so on parameter can be omit.

print (pd.merge(a,b, indicator=True, how='outer')
         .query('_merge=="left_only"')
         .drop('_merge', axis=1))
   0   1
0  1  10
2  3  30

Method 2

You could convert a and b into Indexs, then use the Index.isin method to determine which rows are shared in common:

import pandas as pd
a = pd.DataFrame({0:[1,2,3],1:[10,20,30]})
b = pd.DataFrame({0:[0,1,2,3],1:[0,1,20,3]})

a_index = a.set_index([0,1]).index
b_index = b.set_index([0,1]).index
mask = ~a_index.isin(b_index)
result = a.loc[mask]
print(result)

yields

   0   1
0  1  10
2  3  30

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read