Python Pandas TypeError: first argument must be string or compiled pattern

ε祈祈猫儿з 提交于 2019-12-12 04:53:36

问题


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. 

回答1:


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|\?"

This will check if a question mark OR if 'tourist` is in the string

If you ever want to check regex, pythex is a really good place. I made a test one for you.



来源:https://stackoverflow.com/questions/45943179/python-pandas-typeerror-first-argument-must-be-string-or-compiled-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!