【发布时间】:2019-11-23 16:32:57
【问题描述】:
背景
我有一个示例 df,其中 Text 列包含 0,1,或 >1 ABC's
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 数字(例如ABC: 1111111)更改为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**
问题
如何实现我想要的输出?
【问题讨论】:
标签: python string pandas text apply