Python Pandas 将 String 对象转换为小写并检查字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22909082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas converting String object to lower case and checking for string
提问by user1452759
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?
反正到“HolidayName”栏为小写,然后检查正则表达式转换("Holiday|Recess")
使用.contains
一步到位?
采纳答案by unutbu
private["ISH"] = private.HolidayName.str.contains("(?i)holiday|recess")
The (?i)
in the regex pattern tells the re
module to ignore case.
将(?i)
在正则表达式告诉re
模块忽略大小写。
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:
出现错误的原因是 Series 对象没有该contains
方法;相反,Series.str
属性具有contains
方法。因此,您可以通过以下方式避免错误:
private["ISH"] = private.HolidayName.str.lower().str.contains("holiday|recess")
回答by Bonqus
I'm a bit late to the party, but you could use the keyarg case : bool, default True, If True, case sensitive.
我参加聚会有点晚了,但是您可以使用 keyarg case :bool,默认 True,If True,区分大小写。
private["ISH"] = private.HolidayName.str.contains("holiday|recess", case=False)
public["ISH"] = public.HolidayName.str.contains("holiday|recess", case=False)