Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert string in pandas column using regex if pattern is found

I have a string column in a dataframe and I'd like to insert a # to the begging of my pattern.

For example: My pattern is the letters 'pr' followed by any amount of numbers. If in my column there is a value 'problem in pr123', I would change it to 'problem in #pr123'.

I'm trying a bunch of code snippets but nothing is working for me.

Tried to change the solution to replace for 'pr#123' but this didn't work either.

df['desc_clean'] = df['desc_clean'].str.replace(r'([p][r])(\d+)', r'\1#\2', regex=True)

What's the best way I can replace all values in this column when I find this pattern?

like image 295
eduardoftdo Avatar asked Dec 15 '25 04:12

eduardoftdo


1 Answers

If you need pr#123 you can use

df['desc_clean'] = df['desc_clean'].str.replace(r'(pr)(\d+)', r'\1#\2')

To get #pr123, you can use

df['desc_clean'].str.replace(r'pr\d+', r'#\g<0>')

To match pr as a whole word, you can add a word boundary, \b, in front of pr:

df['desc_clean'].str.replace(r'\bpr\d+', r'#\g<0>')

See the regex demo.

like image 131
Wiktor Stribiżew Avatar answered Dec 16 '25 21:12

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!