Fix Python – Check if string is in a pandas dataframe


Asked By – user2242044

I would like to see if a particular string exists in a particular column within my dataframe.

I’m getting the error

ValueError: The truth value of a Series is ambiguous. Use a.empty,
a.bool(), a.item(), a.any() or a.all().

import pandas as pd

BabyDataSet = [('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]

a = pd.DataFrame(data=BabyDataSet, columns=['Names', 'Births'])

if a['Names'].str.contains('Mel'):
    print ("Mel is there")

Now we will see solution for issue: Check if string is in a pandas dataframe


a['Names'].str.contains('Mel') will return an indicator vector of boolean values of size len(BabyDataSet)

Therefore, you can use

if mel_count>0:
    print ("There are {m} Mels".format(m=mel_count))

Or any(), if you don’t care how many records match your query

if a['Names'].str.contains('Mel').any():
    print ("Mel is there")

This question is answered By – Uri Goren

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0