Bottom Page

Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 extract specific content in a pandas dataframe with a regex?
#1
I'm trying to extract a few words from a large Text field and place result in a new column.
After creating the new column, I'll then run another expression looking for a numerical value between 1 and 29 on either side of the word m_m_s_e. This is a score that I need to capture to another column

I'm stuck at stage 1 - Please help


The following code creates a new column that excludes the target text I want
MMSE_df['targettext'] = MMSE_df['cleannotetext'].str.replace(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}m_m_s_e(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", "")

So I tried the following which throws an error
MMSE_df['targettext'] = MMSE_df['cleannotetext'].str.extract(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}m_m_s_e(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", expand=True)
Error
Error:
ValueError Traceback (most recent call last) <ipython-input-98-93cd99ac572d> in <module>() 1 #MMSE_df['targettext'] = MMSE_df['cleannotetext'].str.replace(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}m_m_s_e(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", "") 2 ----> 3 MMSE_df['targettext'] = MMSE_df['cleannotetext'].str.extract(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}m_m_s_e(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", expand=True) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings.py in extract(self, pat, flags, expand) 1706 @copy(str_extract) 1707 def extract(self, pat, flags=0, expand=None): -> 1708 return str_extract(self, pat, flags=flags, expand=expand) 1709 1710 @copy(str_extractall) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings.py in str_extract(arr, pat, flags, expand) 689 raise ValueError("expand must be True or False") 690 if expand: --> 691 return _str_extract_frame(arr._orig, pat, flags=flags) 692 else: 693 result, name = _str_extract_noexpand(arr._data, pat, flags=flags) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings.py in _str_extract_frame(arr, pat, flags) 581 582 regex = re.compile(pat, flags=flags) --> 583 groups_or_na = _groups_or_na_fun(regex) 584 names = dict(zip(regex.groupindex.values(), regex.groupindex.keys())) 585 columns = [names.get(1 + i, i) for i in range(regex.groups)] C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\strings.py in _groups_or_na_fun(regex) 524 """Used in both extract_noexpand and extract_frame""" 525 if regex.groups == 0: --> 526 raise ValueError("pattern contains no capture groups") 527 empty_row = [np.nan] * regex.groups 528 ValueError: pattern contains no capture groups

I got it working using the following

# A function to get the target test from a Provider notes.
import re
def get_targettext(notes):
   
  #  note_search = re.search(' ([A-Za-z]+)\.', name)
   note_search =  re.search(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}m_m_s_e(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", notes)
    
    # If the title exists, extract and return it.
   if note_search:
        return note_search.group()
   return ""
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  error bars with dataframe and pandas Hucky 4 279 Apr-27-2020, 02:02 AM
Last Post: Hucky
  Python Pandas DataFrame Help AmericanEagle1989 1 181 Apr-12-2020, 12:37 PM
Last Post: AmericanEagle1989
  How does pyplot know what was plotted by the output of pandas.DataFrame(...).cumprod( codeowl 2 194 Mar-28-2020, 08:27 AM
Last Post: j.crater
  Ordering of pandas DataFrame new_to_python 5 270 Mar-15-2020, 06:08 PM
Last Post: new_to_python
  Pandas dataframe merge snmmat 1 257 Mar-09-2020, 06:56 PM
Last Post: jefsummers
  Transform Facebook Graph API insights JSON to pandas dataframe usman 0 274 Mar-03-2020, 05:14 AM
Last Post: usman
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 1,392 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  pandas str.extract multiple regex groups with OR pythonidae 2 1,077 Dec-19-2019, 05:43 PM
Last Post: pythonidae
  Parse XML String in Pandas Dataframe creedX 2 940 Dec-09-2019, 07:35 PM
Last Post: creedX
  Pandas dataframe to join three tables using like condition among them sandeep_ganga 0 406 Nov-29-2019, 08:30 AM
Last Post: sandeep_ganga

Forum Jump:


Users browsing this thread: 1 Guest(s)