2019-08-23
Using regex to alter digits pandas
stackoverflow
Question

Background

I have the following df

import pandas as pd
df = pd.DataFrame({'Text' : ['But the here is \nCase ID: 666666    \nDate is Here 123456 ', 
                                   '999998 For \nCase ID: 123456    \nDate  there', 
                                   'So so \nCase ID: 939393    \nDate hey the 123455 ',],
                      'ID': [1,2,3],
                       'P_ID': ['A','B','C'],

                     })

Output

    ID  P_ID    Text
0   1   A   But the here is \nBase ID: 666666 \nDate is Here 123456
1   2   B   999998 For \nBase ID: 123456 \nDate there
2   3   C   So so \nBase ID: 939393 \nDate hey the 123455

Tried

I have tried the following to **BLOCK** the 6 digits in between \nBase ID: and \nDate

df['New_Text'] = df['Text'].str.replace('ID:(.+?)','ID:**BLOCK**')

And I get the following

  ID P_ID Text New_Text
0               But the here is \nCase ID:**BLOCK**666666 \nDate is Here 123456
1               999998 For \nCase ID:**BLOCK**123456 \nDate there
2               So so \nCase ID:**BLOCK**939393 \nDate hey the 123455

But I don't quite get what I want

Desired Output

  ID P_ID Text New_Text
0               But the here is \nCase ID:**BLOCK** \nDate is Here 123456
1               999998 For \nCase ID:**BLOCK** \nDate there
2               So so \nCase ID:**BLOCK** \nDate hey the 123455

Question

How do I tweak str.replace('ID:(.+?)','ID:**BLOCK**') part of my code to get my desired output?

Answer
1
df['New_Text'] = df['Text'].str.replace(r'ID: *\d+ *', 'ID:**BLOCK** ')

See here for a detailed break-down of the used regex pattern.

Using regex to alter digits pandas
See more ...