I have a column in a dataframe with has free text. I want to replace words starting with AA and ending with AA in the text. Can anyone suggest how to do this?
2 Answers
Here's a simple solution using replace str method and regex pattern
>>> df=pandas.DataFrame({'example':['AAhelloAA','Arreviour','Dunno this is a example of it','a knee','an arrow','AAnother example ofAA']})
>>> print(df)
example
0 AAhelloAA
1 Arreviour
2 Dunno this is a example of it
3 a knee
4 an arrow
5 AAnother example ofAA
>>> df['example'].str.replace(r'(AA).*?(AA)','NEW CHANGE!')
0 NEW CHANGE!
1 Arreviour
2 Dunno this is a example of it
3 a knee
4 an arrow
5 NEW CHANGE!
Name: example, dtype: object
Gotta clarified that the pattern in regex it works in any text that starts and ends with AA.
startswithandendswithmethod. You could also use regular expressions. For this they are perhaps overkill, but in python you will likely need to learn how to use them sooner or later.