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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:58:08  来源:igfitidea点击:

Pandas converting String object to lower case and checking for string

pythonpandas

提问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 .containsin 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 remodule to ignore case.

(?i)在正则表达式告诉re模块忽略大小写。



The reason why you were getting an error is because the Series object does not have the containsmethod; instead the Series.strattribute has the containsmethod. 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)