python - Check if string is in a pandas dataframe -


i see if particular string exists in particular column within dataframe.

i'm getting error

valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().

import pandas pd  babydataset = [('bob', 968), ('jessica', 155), ('mary', 77), ('john', 578), ('mel', 973)]  = pd.dataframe(data=babydataset, columns=['names', 'births'])  if a['names'].str.contains('mel'):     print "mel there" 

a['names'].str.contains('mel') return indicator vector of boolean values of size len(babydataset)

therefore, can use

mel_count=a['names'].str.contains('mel').sum() if mel_count>0:     print ("there {m} mels".format(m=mel_count)) 

or any(), if don't care how many records match query

if a['names'].str.contains('mel').any():     print ("mel there") 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -