Skip to content

Instantly share code, notes, and snippets.

@jkmackie
Created January 1, 2020 04:26
Show Gist options
  • Save jkmackie/b65f75796a731520841b76fe8fbdd0cd to your computer and use it in GitHub Desktop.
Save jkmackie/b65f75796a731520841b76fe8fbdd0cd to your computer and use it in GitHub Desktop.
def match_regex_patt(df, target_col, regex_patt, no_match_value='None'):
'''Returns regex_patt matches as list. Case is ignored.'''
matchList=[]
cnt = 0
for idx in df.index:
m = re.search(regex_patt, df.loc[idx, target_col], flags=re.IGNORECASE) #re.search(pattern, string) gets first match
if m is not None:
matchList.append(m.group(1).lower())
else:
cnt+=1
matchList.append(no_match_value)
print(f'In {target_col}, {cnt} missed matches for regex pattern: {regex_patt}.')
return matchList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment