Pandas converting String object to lower case and checking for string

Question:

I have the below 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 anyway to convert the ‘HolidayName’ column to lower case and then check the regular expression ("Holiday|Recess")using .contains in one step?

Asked By: user1452759

||

Answers:

private["ISH"] = private.HolidayName.str.contains("(?i)holiday|recess")

The (?i) in the regex pattern tells the re module to ignore case.


The reason why you were getting an error is because the Series object does not have the contains method; instead the Series.str attribute has the contains method. So you could avoid the error with:

private["ISH"] = private.HolidayName.str.lower().str.contains("holiday|recess")
Answered By: unutbu

I’m a bit late to the party, but you could use the keyarg
case : bool, default True, If True, case sensitive.

private["ISH"] = private.HolidayName.str.contains("holiday|recess", case=False)
public["ISH"] = public.HolidayName.str.contains("holiday|recess", case=False)
Answered By: Bonqus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.