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:

Example: determine Pandas Column contains a particular value

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.