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
Tue, 02/16/2021 - 08:07

Authored by

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