How to Check value exist in Pandas DataFrame

Pandas

In this post, we are going to learn how to check the value that exists in Pandas’ dataframe. We are going to check single or multiple elements that exist in the dataframe by using IN and NOT IN operator,isin() method.

1. Check single element exist in Dataframe


To check a given value exists in the dataframe we are using IN operator with if statement. If the value exists then it returns True else False.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,98,99],
    'Subject': ['Math','Math','Math']
}
 
df = pd.DataFrame(Student_dict)

if 98 in df.values:

 print('value exist in Dataframe')
else :    
  print('value does not exist in Dataframe')

Output

value exist in Dataframe

2. Not IN to Check single element exist in Dataframe


To check a given value exists in the dataframe we are using the Not IN operator with an if statement. If the value exists then it returns False else True based on the return value we are printing the message.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,98,99],
    'Subject': ['Math','Math','Math']
}
 
df = pd.DataFrame(Student_dict)

if 98 not in df.values:

 print('value does not exist in Dataframe')
else :    
  print('value exist in Dataframe')

Output

value exist in Dataframe

3. dict compreshesion to Check mutiple element exist in dataframe


To check if multiple-element exist in the dataframe we are using the dictionary comprehension and its dictionary of key-value pairs. It returns TRUE if a value exists FALSE if does not exist.

Program Example

import pandas as pd


Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,98,99],
    'Subject': ['Math','Math','Math']
}


 
df = pd.DataFrame(Student_dict)


#list of elements to check
list_items = ['Jack','Rack','Max']


#calling above define function
result_dict = {item: True if item in df.values else False for item in list_items}

print(result_dict)



Output

{'Jack': True, 'Rack': True, 'Max': True}

4. isin() Method to check value exist in dataframe


The isin() method is used to check single or multiple elements exist in the dataframe.

Syntax

dataframe.isin(values)

Parameters

values :It can be iterable,series,dataframe,dict.

Return value: It returns a boolean dataframe in which each value is represented with true for match value False for unmatched.

isin() method return values Program Example

import pandas as pd



 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,100,100],
    'Subject': ['Math','Math','Math']
}


 
df = pd.DataFrame(Student_dict)


result_df = df.isin([100])
print(result_df)

Output

   Name  Marks  Subject
0  False   True    False
1  False   True    False
2  False   True    False

4.1 Check single element is in dataframe


The any() method returns a pandas series that displays a column that contains True OR FALSE for given values. The name column returns true because it contains the given value ‘Jack’.Let us understand with the below example.

Program Example

import pandas as pd



 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,100,100],
    'Subject': ['Math','Math','Math']
}


 
df = pd.DataFrame(Student_dict)


result = df.isin(['Jack']).any()

print(result)

Output

Name        True
Marks      False
Subject    False
dtype: bool

In this example, we have used any() method two times. The first any() method returns a pandas series that displays a column that contains True OR FALSE for given values.

The second any() call on return series returns a single boolean value. When boolean value TRUE value exists in dataframe else not.

Program Example

import pandas as pd



 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,100,100],
    'Subject': ['Math','Math','Math']
}


 
df = pd.DataFrame(Student_dict)


result = df.isin(['Jack']).any().any()

if result :
    print('Value  exist in dataframe')
else:
 print('Value does not exist in dataframe')

Output

Value  exist in dataframe

4.2 Check if column contain from list in dataframe


To check if any of the values in the given list exist in the dataframe. We can pass the list to isin() method and it works the same as the above explanation. If the value exists it returns TRUE else False

Program Example

import pandas as pd



 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,100,100],
    'Subject': ['Math','Math','Math']
}


 
df = pd.DataFrame(Student_dict)


result = df.isin(['Jack',100,'Math']).any().any()

if result :
    print('Value  exist in dataframe')
else:
 print('Value does not exist in dataframe')

Output

Value  exist in dataframe

Summary

In this post, we have understood multiple ways of How to Check value exists in Pandas DataFrame with code example.