Python Pandas TypeError:第一个参数必须是字符串或编译模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45943179/
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
Python Pandas TypeError: first argument must be string or compiled pattern
提问by jeangelj
I am sorry for the super easy question, but I can't make it work
我很抱歉这个超级简单的问题,但我不能让它工作
I am cleaning data and want to add a flag, if the name (which is seperate into two columns First and Last Name) is wrong. I established multiple patterns, but for now I was working with seperate statements, can I merge all of those statements into one?
我正在清理数据并想添加一个标志,如果名称(分为两列名字和姓氏)是错误的。我建立了多个模式,但现在我正在处理单独的语句,我可以将所有这些语句合并为一个吗?
pattern = "\?"
match = incremental['First_Name'].str.contains(pattern) | incremental['Last_Name'].str.contains(pattern)
incremental['Name_Flag'] = np.where(match, 'Y', '')
pattern = "tourist"
match = incremental['First_Name'].str.contains(pattern) | incremental['Last_Name'].str.contains(pattern)
incremental['Name_Flag'] = np.where(match, 'Y', '')
This doesn't work, because the second statement over-writes the first.
这不起作用,因为第二个语句覆盖了第一个语句。
pattern = ("tourist","/?")
match = incremental['First_Name'].str.contains(pattern) | incremental['Last_Name'].str.contains(pattern)
incremental['Name_Flag'] = np.where(match, 'Y', '')
I get an error for the second version (not surprisingly)
我收到第二个版本的错误(不足为奇)
TypeError: first argument must be string or compiled pattern.
回答by MattR
IF you are trying to look for both regex patterns- as in search for both ?
and tourist
in the string. you can use the |
operator. So change pattern
to
如果你正在试图寻找这两个正则表达式patterns-在搜索都?
与tourist
在该字符串。您可以使用|
运算符。所以pattern
改为
pattern = "tourist|\?"
This will check if a question mark ORif 'tourist` is in the string
这将检查字符串中是否有问号或'tourist`
If you ever want to check regex, pythexis a really good place. I made a test one for you.
如果您想检查正则表达式,pythex是一个非常好的地方。我做了一个测试给你。