Skip to main content

Pandas DataFrame Difference Operation

In Pandas DataFrame Set Operation difference of two sets can be obtained using an isin() method.We can implement this isin() method in tandem with boolean indexing .This method will help us in d=finding the row which belongs to one dataframe but does not belong to any other dataframe.

Example

import pandas as pd
A= pd.DataFrame ({"name":["Jack","Alice"],
"Roll_No":[13,15]})
print(A)
#First DataFrame

Output

    name  Roll_No
0   Jack       13
1  Alice       15
B= pd.DataFrame ({"name":["Jack","Monica"],
"Roll_No":[13,19]})
print(B)

Output

     name  Roll_No
0    Jack       13
1  Monica       19
result_0=A[A.Roll_No.isin(B.Roll_No) == False]
result_0#A-B
  name Roll_No
1 Alice 15
result_1=B[B.Roll_No.isin(A.Roll_No) == False]
result_1#B-A
  name Roll_No
1 Monica 19
Tags
Submitted by devanshi.srivastava on February 16, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.