【发布时间】:2019-12-22 04:24:03
【问题描述】:
背景
这个问题是Alter text in pandas column based on names 的变体。
我有以下df 故意有各种问题
import pandas as pd
df = pd.DataFrame({'Text' : ['But now Smith,J J is Here from Smithsville',
'Maryland is HYDER,A MARY Found here ',
'hey here is Annual Doe,Jane Ann until ',
'The tuckered was Tucker,Tom is Not here but'],
'P_ID': [1,2,3,4],
'P_Name' : ['SMITH,J J', 'HYDER,A MARY', 'DOE,JANE ANN', 'TUCKER,TOM T'],
'N_ID' : ['A1', 'A2', 'A3', 'A4']
})
输出
N_ID P_ID P_Name Text
0 A1 1 SMITH,J J But now Smith,J J is Here from Smithsville
1 A2 2 HYDER,A MARY Maryland is HYDER,A MARY Found here
2 A3 3 DOE,JANE ANN hey here is Annual Doe,Jane Ann until
3 A4 4 TUCKER,TOM T The tuckered was Tucker,Tom is Not here but
目标
1) 对于P_Name 中的名称,例如SMITH,J J 块名称与 **BLOCK** 在对应的 Text 列中
2) 创建New_Text 列
期望的输出
N_ID P_ID P_Name Text New_Text
0 But now **BLOCK** is Here from Smithsville
1 Maryland is **BLOCK** Found here
2 hey here is Annual **BLOCK** until
3 The tuckered was **BLOCK** is Not here but
问题
如何实现我想要的输出?
【问题讨论】:
标签: regex python-3.x pandas text replace