ADVERTISEMENT
ADVERTISEMENT

How to Use 'NOT IN' Filter in Pandas?

Learn how to use 'NOT IN' filter in Pandas?
Submitted by Pranit Sharma, on April 19, 2022

The "NOT IN" the filter is used to check whether a particular data is available in the DataFrame or not. The "NOT IN" the condition in pandas is checked by using the DataFrame.isin() operator.

The DataFrame.isin() operator is used to filter some specified data from the entire dataset using a list of elements that need to be excluded to filter the data.

Syntax:

DataFrame[~DataFrame[column_name].isin(list)]

Parameter: It takes DataFrame as a parameter. Secondly, it takes the column name and the list of elements that needs to be excluded.

To work with MultiIndex in Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Let us understand with the help of an example.

# Importing pandas package
import pandas as pd

# Creating a dictionary of student marks
d = {
    "Peter":[65,70,70,75],
    "Harry":[45,56,66,66],
    "Tom":[67,87,65,53],
    "John":[56,78,65,64]
}

# Now, Create DataFrame and assign index name 
# as subject names
df = pd.DataFrame(d,index=["Maths","Physics","Chemistry","English"])

# Printing the DataFrame
print(df,"\n")

# For filtering data we need to assign a list of 
# elements which we want to exclude
# Creating a list of single element where element = 70 
# which mean we need to exclude those rows where marks 
# are 70 and column value is peter
list = [70]

# Applying Filter in Peter column
print(df[~df['Peter'].isin(list)])

Output:

           Peter  Harry  Tom  John
Maths         65     45   67    56
Physics       70     56   87    78
Chemistry     70     66   65    65
English       75     66   53    64 

         Peter  Harry  Tom  John
Maths       65     45   67    56
English     75     66   53    64

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.