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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd
  
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya'
              'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
  
# creating a Dataframe object 
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f'])
  
print("Dataframe: \n\n", df)
  
# chcek 'Ankit' exist in dataframe or not
if 'Ankit' in df.values :
    print("\nThis value exists in Dataframe")
  
else :
    print("\nThis value does not exists in Dataframe")

chevron_right


Output :

Method 2: Use not in operator to check if an element doesn’t exists in dataframe.



filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd
  
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
  
# creating a Dataframe object 
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f'])
  
print("Dataframe: \n\n", df)
  
# chcek 'Ankit' exist in dataframe or not
if 'Ankita' not in df.values :
    print("\nThis value not exists in Dataframe")
  
else :
    print("\nThis value exists in Dataframe")
     

chevron_right


Output :

python-isin-2

Method 3 : Check if a single element exist in Dataframe using isin() method of dataframe.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd
  
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
  
# creating a Dataframe object 
df = 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 value
res = df.isin(['Ankit']).any().any()
  
if res :
    print("\nThis value exists in Dataframe")
  
else :
    print("\nThis value does not exists in Dataframe")

chevron_right


Output :

python-isin-4

Method 4 : Check if any of the given values exists in the Dataframe using isin() method of dataframe.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd
  
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
  
# creating a Dataframe object 
df = 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 value
res = 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")

chevron_right


Output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.