Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas converting String object to lower case and checking for string

Tags:

python

pandas

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?

like image 869
user1452759 Avatar asked Apr 07 '14 09:04

user1452759


People also ask

How would you get a lowercase version of a string?

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.

How do you convert a string into lowercase in python?

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.

How do I check if a string contains a substring panda?

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") .


2 Answers

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) 
like image 30
Bonqus Avatar answered Sep 21 '22 14:09

Bonqus


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") 
like image 62
unutbu Avatar answered Sep 20 '22 14:09

unutbu