Q:

How to Use \'NOT IN\' Filter in Pandas?

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Import multiple csv files into pandas and concaten... >>
<< How to widen output display to see more columns in...