背景
我有一个样本df
,其中Text
列包含0、1或>;1 ABC
import pandas as pd
df = pd.DataFrame({'Text' : ['Jon J Mmith ABC: 1111111 is this here',
'ABC: 1234567 Mary Lisa Rider found here',
'Jane A Doe is also here',
'ABC: 2222222 Tom T Tucker is here ABC: 2222222 too'],
'P_ID': [1,2,3,4],
'N_ID' : ['A1', 'A2', 'A3', 'A4']
})
#rearrange columns
df = df[['Text','N_ID', 'P_ID']]
df
Text N_ID P_ID
0 Jon J Mmith ABC: 1111111 is this here A1 1
1 ABC: 1234567 Mary Lisa Rider found here A2 2
2 Jane A Doe is also here A3 3
3 ABC: 2222222 Tom T Tucker is here ABC: 2222222... A4 4
目标
1)将Text
列(例如ABC: 1111111
)中的ABC
数字更改为ABC: **BLOCK**
2)创建包含此输出的新列Text_ABC
所需输出
Text N_ID P_ID Text_ABC
0 Jon J Mmith ABC: 1111111 is this here A1 1 Jon J Mmith ABC: **BLOCK** is this here
1 ABC: 1234567 Mary Lisa Rider found here A2 2 ABC: **BLOCK** Mary Lisa Hider found here
2 Jane A Doe is also here A3 3 Jane A Doe is also here
3 ABC: 2222222 Tom T Tucker is here ABC: 2222222 A4 4 ABC: **BLOCK** Tom T Tucker is here ABC: **BLOCK**
问题
我如何实现我想要的输出?你知道吗
如果要替换所有数字,可以执行以下操作:
但是如果您想更具体一些,并且只替换
ABC:
之后的数字,那么您可以使用以下方法:给你:
作为正则表达式,} 中使用它表示“用
\d+
表示“匹配一个或多个连续数字”,因此在^{***BLOCK***
替换一个或多个连续数字”相关问题 更多 >
编程相关推荐