Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is in a pandas dataframe

Tags:

python

pandas

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") 
like image 262
user2242044 Avatar asked Jun 19 '15 18:06

user2242044


People also ask

How do you check if a column contains a certain string?

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.

How do you know if a DataFrame column contains a value?

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.


2 Answers

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") 
like image 50
Uri Goren Avatar answered Oct 20 '22 01:10

Uri Goren


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 
like image 42
Zero Avatar answered Oct 20 '22 01:10

Zero