0

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. 
jeangelj
  • 4,338
  • 16
  • 54
  • 98
  • 1
    I'm slightly confused. Can you add some sample input and expected output? Are you looking to combine the regex pattern? looking to check for both patterns? What is the end-goal question? – MattR Aug 29 '17 at 16:28

1 Answers1

4

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.

MattR
  • 4,887
  • 9
  • 40
  • 67