Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I apply a regex substitution in a string column of a DataFrame?

I have a DataFrame called "Animals" that looks like this:

 Words
 The Black Cat
 The Red Dog

I want to add a plus sign before each word so that it looks like this:

 Words
 +The +Black +Cat
 +The +Red +Dog

I have tried this using regex but it did not work:

 df = re.sub(r'([a-z]+)', r'+\1', Animals)
like image 902
user3682157 Avatar asked Sep 09 '15 19:09

user3682157


1 Answers

You can use str.replace with the following regex to make the change to all rows of the column:

df.Words = df.Words.str.replace(r'(\b\S)', r'+\1')

The DataFrame then looks like this:

>>> df
              Words
0  +The +Black +Cat
1    +The +Red +Dog
like image 143
Alex Riley Avatar answered Sep 22 '22 20:09

Alex Riley