Home »
Python »
Python Programs
How to determine whether a Pandas Column contains a particular value?
Given a Pandas DataFrame, we have to determine whether its Column contains a particular value.
Submitted by Pranit Sharma, on May 23, 2022
For this purpose, we will use a simple python keywords 'in' & 'not in'. These keywords are used to check whether a value is present in a series or collection or not.
Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. Here, we are going to check the whether a value is present in a column or not.
Let us understand with the help of an example,
# Import pandas Package
import pandas as pd
# Creating dictionary
d = {
'Name':['Ankit', 'Tushar', 'Saloni','Jyoti', 'Anuj', 'Rajat'],
'Age':[23, 21, 22, 21, 24, 25],
'University':['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu']
}
# Creating a Dataframe
df = pd.DataFrame(d,index = ['a', 'b', 'c', 'd', 'e', 'f'])
print("Created Dataframe:\n", df)
# check 'Jyoti' exist in DataFrame or not
if 'Jyoti' in df.values :
print("\nYes,'Jyoti' is Present in DataFrame")
else :
print("\nSorry!, This value does not exists in Dataframe")
Output:
Python Pandas Programs »