问题
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