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?
The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.
In Python, lower() is a built-in method used for string handling. The lower() method returns the lowercased string from the given string. It converts all uppercase characters to lowercase.
Using “contains” to Find a Substring in a Pandas DataFrame The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not. A basic application of contains should look like Series. str. contains("substring") .
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)
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")
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