How to Use “NOT IN” Filter in Pandas? - GeeksforGeeks

22 Nov, 2021
In this article, we will discuss NOT IN filter in pandas, NOT IN is a membership operator used to check whether the data is present in dataframe or not. It will return true if the value is not  present, otherwise false
Python3
# import pandas moduleimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]}, index=[0, 1, 2])  # displaydata1
Output:
sample dataframe
We are using isin() operator to get  the given values in the dataframe and those values are taken from the list, so we are filtering the dataframe one column values which are present in that list.
Syntax: dataframe[~dataframe[column_name].isin(list)]
where
dataframe is the input dataframe
column_name is the column that is filtered
list is the list of values to be removed in that column
Python3
# import pandas moduleimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]}, index=[0, 1, 2])  # consider a listlist1 = ['harsha', 'jyothika']  # filter in name columnprint(data1[~data1['name'].isin(list1)])print("============")  # consider a listlist2 = ['R']    # filter in name columnprint(data1[~data1['subject1'].isin(list2)])print("============")  # consider a listlist3 = [96, 89]  # filter in name columnprint(data1[~data1['marks'].isin(list3)])
Output:
NOT IN Filter with One Column
Now we can filter in more than one column by using any() function. This function will check the value that exists in any given column and columns are given in [[]] separated by a comma.
Syntax: dataframe[~dataframe[[columns]].isin(list).any(axis=1)]
Python3
# import pandas moduleimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]}, index=[0, 1, 2])  # consider a listlist1 = ['harsha', 'jyothika', 96]  # filter in name and marks columnprint(data1[~data1[['name', 'marks']].isin(list1).any(axis=1)])print("============")  # consider a listlist2 = ['R', 'sravan']  # filter in name and subject1 columnprint(data1[~data1[['subject1', 'name']].isin(list2).any(axis=1)])
Output:
 NOT IN Filter with Multiple Column
This is similar to the above functionality.
Syntax: dataframe[~numpy.isin(dataframe[‘column’], list)]
Python3
# import pandas moduleimport numpy as npimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]}, index=[0, 1, 2])  # consider a listlist1 = ['harsha', 'jyothika', 96]  # filter in name columndata1[~np.isin(data1['name'], list1)]
Output:
numpy with NOT IN filter
Picked
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list