Python Forum
pandas str.extract multiple regex groups with OR
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas str.extract multiple regex groups with OR
#3
Unfortunately the text contains other unrelated numbers, such as 25 items, 2" long, 4 inches deep so I only want the values when they match the regex I provided. I also seem to have a common use case for "OR" regex group matching for extracting other data (e.g. extracting an ID from a text field when it takes one or another discreet pattern). The other way I see to achieve it is to run str.extract for each group creating as many new columns as match groups, and then combine these afterwards. This just seemed inefficient, but perhaps this is the only way possible with str.extract.

regex = r'(\d)\"\s*deep|(\d)\"\s*depth|(\d)\sinches\sdeep')
df['depth1']=df['text'].str.extract(r'(\d)\"\s*deep')
df['depth2']=df['text'].str.extract(r'(\d)\"\s*depth')
df['depth3']=df['text'].str.extract(r'(\d)\sinches\sdeep')

df['depth_final'] = df['depth1'].where(df['depth1'].notnull(), df['depth2'])
df['depth_final'] = df['depth_final'].where(df['depth_final'].notnull(), df['depth3'])
df = df.drop(['depth1','depth2', 'depth3'],axis=1)
Reply


Messages In This Thread
RE: pandas str.extract multiple regex groups with OR - by pythonidae - Dec-19-2019, 05:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Import multiple CSV files into pandas Krayna 0 1,694 May-20-2021, 04:56 PM
Last Post: Krayna
  Weighted average with multiple weights and groups amyd 0 2,090 Oct-11-2019, 10:30 AM
Last Post: amyd
  Reading Multiple Sheets using Pandas dhiliptcs 1 4,011 Sep-30-2019, 11:26 PM
Last Post: scidam
  Handling multiple errors when using datafiles in Pandas alphanov 1 1,821 Jul-16-2019, 03:17 AM
Last Post: scidam
  How to extract different data groups from multiple CSV files using python Rafiz 3 3,197 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  extract specific content in a pandas dataframe with a regex? steve1040 0 13,510 Oct-05-2017, 03:17 AM
Last Post: steve1040

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020