Pandas Converting String Object to Lower Case and String Check

I have the following code

import pandas as pd private = pd.read_excel("file.xlsx","Pri") public = pd.read_excel("file.xlsx","Pub") private["ISH"] = private.HolidayName.str.lower().contains("holiday|recess") public["ISH"] = public.HolidayName.str.lower().contains("holiday|recess") 

I get the following error:

 AttributeError: 'Series' object has no attribute 'contains' 

Is there a way to convert the 'HolidayName' column to lowercase and then check the regex ("Holiday|Recess") using .contains in one step?

+18
source share
2 answers
 private["ISH"] = private.HolidayName.str.contains("(?i)holiday|recess") 

(?i) in the regex pattern, tells re ignore case.


The reason you got the error is because the Series object does not have a contains method; instead, the Series.str attribute has a contains method. This way you can avoid the error:

 private["ISH"] = private.HolidayName.str.lower().str.contains("holiday|recess") 
+32
source

I was a bit late to the party, but you could use the case of keys: bool, True by default, if True, case sensitive.

 private["ISH"] = private.HolidayName.str.contains("holiday|recess", case=False) public["ISH"] = public.HolidayName.str.contains("holiday|recess", case=False) 
+4
source

All Articles