0

I'm trying to find the first occurance of a pattern in a string for each string in a pandas column. Here's what I have so far:

drilling_df['rig_number'] = 
drilling_df['contractor_name'].apply(search_contractor_name)

def search_contractor_name(name):
    test = re.search(
        r'(?!^)(?<!\bNo\.\s)(?<!\bDivision of\s)(?<!\d)(?!(?:19[6-9][0-9]|20[01][0-9]|2020)(?!\d))(\d+(?!\d)e?)',
        name)
    if test:
        return test.group()
    else:
        return "" 

I ran this code but I keep getting this error:

TypeError: expected string or bytes-like object

I can't seem to figure out what I'm doing wrong. Thanks in advance for any help.

DataScience99
  • 339
  • 3
  • 10
  • Try printing the `name` parameter before searching it as a debug. Seems like its not a string – GandhiGandhi Jul 08 '19 at 19:25
  • Is the line break in your first and second lines present in your code? Are you defining your function `search_contractor_name` *after* attempting to `.apply()` it? – Brendan Jul 08 '19 at 19:31
  • 1
    @GandhiGandhi One of the names was nan and that's what was causing the error, thanks a lot for the help! – DataScience99 Jul 08 '19 at 20:22

0 Answers0