【问题标题】:alter characters in between two defined words更改两个已定义单词之间的字符
【发布时间】:2019-12-22 06:40:44
【问题描述】:

背景

我有以下 df

import pandas as pd
df = pd.DataFrame({'Text' : ['But the here is SERG BATH # : S00-1111 MR # 111 is Here ', 
                                   'Found here SERG BATH # : E22-22222 MR # 000', 
                                   'So so SERG BATH # : L88-888 MR # 975 hey the ',
                                'The SERG BATH # : V99-99 MR # 232 here but',
                              'The is not here is the SERG BATH # : A33-3 MR # 212 here and'],


                      'ID': [1,2,3,4,5],
                       'P_ID': ['A','B','C','D','E'],

                     })

目标

1) 屏蔽SERG BATH # :MR # 之间的所有字符

2) 创建新列New_Text

示例

改变

"SERG BATH # : A33-3 MR #" 

进入

"SERG BATH # : **BLOCK** MR #"

期望的输出

   ID P_ID  Text  New_Text
0                 "But the here is SERG BATH # : **BLOCK** MR # 111 is Here"
1                 "Found here SERG BATH # : **BLOCK**  MR # 000"
2                 "So so SERG BATH # : **BLOCK** MR # 975 hey the"
3                 "The SERG BATH # : **BLOCK**  MR # 232 here but"
4                 "The is not here is the SERG BATH # : **BLOCK**  MR # 212 here and"

【问题讨论】:

  • 你试过什么?编辑问题以包含您的尝试

标签: regex python-3.x pandas text replace


【解决方案1】:

试试:

df['New_Text'] = df['Text'].str.replace('BATH \# \:(.+?)MR \#','BATH #  :*** Block *** MR #')

【讨论】:

    猜你喜欢
    • 2016-12-06
    • 2020-06-16
    • 2012-12-31
    • 2014-09-23
    • 2015-04-08
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    相关资源
    最近更新 更多