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")
Using “contains” to Find a Substring in a Pandas DataFrame The contains method in Pandas allows you to search a column for a specific substring. The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not.
You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas.
a['Names'].str.contains('Mel')
will return an indicator vector of boolean values of size len(BabyDataSet)
Therefore, you can use
mel_count=a['Names'].str.contains('Mel').sum() 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")
You should use any()
In [98]: a['Names'].str.contains('Mel').any() Out[98]: True In [99]: if a['Names'].str.contains('Mel').any(): ....: print "Mel is there" ....: Mel is there
a['Names'].str.contains('Mel')
gives you a series of bool values
In [100]: a['Names'].str.contains('Mel') Out[100]: 0 False 1 False 2 False 3 False 4 True Name: Names, dtype: bool
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With