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
#1
Hi. I have a column of text with inputs like, 3" deep, 4 inches deep, 5" depth. I wanted to extract just the value and put this in a new column.

From regex101.com, I confirm the following captures any of these for all my examples:
r'(\d)\"\s*deep|(\d)\"\s*depth|(\d)\sinches\sdeep'
However, when I try to use this regex pattern with str.extract as follows
df['depth']=df['text'].str.extract(r'(\d)\"\s*deep|(\d)\"\s*depth|(\d)\sinches\sdeep')
Error:
ValueError: Wrong number of items passed 3, placement implies 1
The docs say it should just return the first match. I tried str.extractall just in case, but this produces
Error:
TypeError: incompatible index of inserted column with frame index
How do I pass regex OR patterns to str.extract? Or, how should I match each of these cases to create a new column?

Thanks!
Reply


Messages In This Thread
pandas str.extract multiple regex groups with OR - by pythonidae - Dec-19-2019, 09:38 AM

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