Extracting words from column in pandas df using regex

Viewed 92

I have a pandas df column that contains English words, some were entered as eg. Ant(small,white). I want to remove the bracketed words and put the in a new column. The code I used is written below

pattern = re.compile('.*\((\w+)\).*')
df_new['Context']= [re.search(pattern,i) for i in df_new.English_words.values]

The screenshot shows the way the table looks 1

The output of the code

The output of the code

1 Answers

You can use

# Extract all occurrences of strings inside parentheses and join them with comma + space
df_new['Context'] = df_new['English_words'].str.findall(r'\(([^()]+)\)').str.join(', ')
# Remove them from the original column
df_new['English_words'] = df_new['English_words'].str.replace(r'\s*\([^()]+\)', '')

Details:

  • .str.findall(r'\(([^()]+)\)').str.join(', ') - the findall part extracts all substrings between brackets and then str.join creates a single string out of the matches using space+comma as a separator
  • .str.replace(r'\s*\([^()]+\)', '') - removes all occurrences of zero or more whitespaces and then a substring between parentheses.
Related