[英]including word boundary in string modification to be more specific
背景
以下是对跳过空列表的修改和继续功能的微小更改
import pandas as pd
Names = [list(['ann']),
list([]),
list(['elisabeth', 'lis']),
list(['his','he']),
list([])]
df = pd.DataFrame({'Text' : ['ann had an anniversery today',
'nothing here',
'I like elisabeth and lis 5 lists ',
'one day he and his cheated',
'same here'
],
'P_ID': [1,2,3, 4,5],
'P_Name' : Names
})
#rearrange columns
df = df[['Text', 'P_ID', 'P_Name']]
df
Text P_ID P_Name
0 ann had an anniversery today 1 [ann]
1 nothing here 2 []
2 I like elisabeth and lis 5 lists 3 [elisabeth, lis]
3 one day he and his cheated 4 [his, he]
4 same here 5 []
以下代码有效
m = df['P_Name'].str.len().ne(0)
df.loc[m, 'New'] = df.loc[m, 'Text'].replace(df.loc[m].P_Name,'**BLOCK**',regex=True)
并做以下事情
1)使用P_Name
的名称通过放置**BLOCK**
来阻止Text
列中的相应文本
2)生成一个新列New
如下所示
Text P_ID P_Name New
0 **BLOCK** had an **BLOCK**iversery today
1 NaN
2 I like **BLOCK** and **BLOCK** 5 **BLOCK**ts
3 one day **BLOCK** and **BLOCK** c**BLOCK**ated
4 NaN
问题
但是,这段代码有点“太好了”。
使用P_Name
['his','he']
来阻止Text
:
例如: one day he and his cheated
变成one day **BLOCK** and **BLOCK** c**BLOCK**ated
渴望: one day he and his cheated
变成one day **BLOCK** and **BLOCK** cheated
在这个例子中,我想cheated
作为cheated
而不是成为c**BLOCK**ated
期望的输出
Text P_ID P_Name New
0 **BLOCK** had an anniversery today
1 NaN
2 I like **BLOCK** and **BLOCK**5 lists
3 one day **BLOCK** and **BLOCK** cheated
4 NaN
题
如何实现所需的输出?
您需要为df.loc[m].P_Name
列表中的每个字符串添加单词边界,如下所示:
s = df.loc[m].P_Name.map(lambda x: [r'\b'+item+r'\b' for item in x])
Out[71]:
0 [\bann\b]
2 [\belisabeth\b, \blis\b]
3 [\bhis\b, \bhe\b]
Name: P_Name, dtype: object
df.loc[m, 'Text'].replace(s, '**BLOCK**',regex=True)
Out[72]:
0 **BLOCK** had an anniversery today
2 I like **BLOCK** and **BLOCK** 5 lists
3 one day **BLOCK** and **BLOCK** cheated
Name: Text, dtype: object
有时循环是很好的做法
df['New']=[pd.Series(x).replace(dict.fromkeys(y,'**PHI**') ).str.cat(sep=' ')for x , y in zip(df.Text.str.split(),df.P_Name)]
df.New.where(df.P_Name.astype(bool),inplace=True)
df
Text ... New
0 ann had an anniversery today ... **PHI** had an anniversery today
1 nothing here ... NaN
2 I like elisabeth and lis 5 lists ... I like **PHI** and **PHI** 5 lists
3 one day he and his cheated ... one day **PHI** and **PHI** cheated
4 same here ... NaN
[5 rows x 4 columns]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.