Check if a value exists in a DataFrame using in & not in operator in Python-Pandas - GeeksforGeeks

11 Jun, 2021
In this article, Let’s discuss how to check if a given value exists in the dataframe or not.Method 1 : Use in operator to check if an element exists in dataframe.  
Python3
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya',              'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],} # creating a Dataframe objectdf = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],                  index = ['a', 'b', 'c', 'd', 'e', 'f']) print("Dataframe: \n\n", df) # check 'Ankit' exist in dataframe or notif 'Ankit' in df.values :    print("\nThis value exists in Dataframe") else :    print("\nThis value does not exists in Dataframe")
Output :  
Method 2: Use not in operator to check if an element doesn’t exists in dataframe.  
Python3
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],} # creating a Dataframe objectdf = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],                  index = ['a', 'b', 'c', 'd', 'e', 'f']) print("Dataframe: \n\n", df) # check 'Ankit' exist in dataframe or notif 'Ankita' not in df.values :    print("\nThis value not exists in Dataframe") else :    print("\nThis value exists in Dataframe")    
Output :  
Method 3 : Check if a single element exist in Dataframe using isin() method of dataframe. 
Python3
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],} # creating a Dataframe objectdf = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],                  index = ['a', 'b', 'c', 'd', 'e', 'f']) print("Dataframe: \n\n", df) # isin() methods return Boolean# Dataframe of given Dimension# first any() will return boolean series# and 2nd any() will return single bool valueres = df.isin(['Ankit']).any().any() if res :    print("\nThis value exists in Dataframe") else :    print("\nThis value does not exists in Dataframe")
Output :  
Method 4 : Check if any of the given values exists in the Dataframe using isin() method of dataframe.  
Python3
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],    'Age' : [23, 21, 22, 21, 24, 25],    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],} # creating a Dataframe objectdf = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],                  index = ['a', 'b', 'c', 'd', 'e', 'f']) print("Dataframe: \n\n", df) # isin() methods return Boolean Dataframe# of given Dimension first any() will return# boolean series and 2nd any() will return# single boolean valueres = df.isin(['Ankit', 'good', 30]).any().any() if res :    print("\nany of the mention value exists in Dataframe")     else :    print("\nNone of thses values exists in Dataframe")
Output :  
 
surinderdawra388
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
sum() function in Python
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe
*args and **kwargs in Python
Graph Plotting in Python | Set 1
Print lists in Python (4 Different Ways)