问题描述
我很抱歉这个超级简单的问题,但我不能让它工作
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.
推荐答案
如果您正在尝试同时查找两种正则表达式模式 - 例如同时查找 ?
和 tourist
在字符串中.您可以使用 |
运算符.所以将 pattern
改为
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
pattern = "tourist|\?"
这将检查问号是否OR如果'tourist`在字符串中
This will check if a question mark OR if 'tourist` is in the string
如果你想检查正则表达式,pythex 是一个非常好的地方.我为你做了一个测试.
If you ever want to check regex, pythex is a really good place. I made a test one for you.
这篇关于Python Pandas TypeError:第一个参数必须是字符串或编译模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!