【发布时间】:2019-12-06 03:02:42
【问题描述】:
背景
以下代码由skipping empty list and continuing with function稍作修改
import pandas as pd
Names = [list(['Jon', 'Smith', 'jon', 'John']),
list([]),
list(['Bob', 'bobby', 'Bobs']),
list([]),
list([])]
df = pd.DataFrame({'Text' : ['Jon J Smith is Here and jon John from ',
'get nothing from here',
'I like Bob and bobby and also Bobs diner ',
'nothing here too',
'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 Jon J Smith is Here and jon John from 1 [Jon, Smith, jon, John]
1 get nothing from here 2 []
2 I like Bob and bobby and also Bobs diner 3 [Bob, bobby, Bobs]
3 nothing here too 4 []
4 same here 5 []
工作代码
以下代码取自skipping empty list and continuing with function
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)
并在df 中生成以下New 列
Text P_ID P_Name New
0 **BLOCK** J **BLOCK** is Here and **BLOCK** **BLOCK** ...
1 NaN
2 I like **BLOCK** and **BLOCK** and also **BLOCK** d..
3 NaN
4 NaN
期望的输出
但是,我想保留原始文本,而不是 1、3、4 行中的 NaN。 get nothing from here 如下图所示
Text P_ID P_Name New
0 **BLOCK** J **BLOCK** is Here and **BLOCK** **BLOCK** ...
1 get nothing from here
2 I like **BLOCK** and **BLOCK** and also **BLOCK** d..
3 nothing here too
4 same here
问题
如何调整下面的代码以实现我想要的输出?
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)
【问题讨论】:
标签: python-3.x string pandas text empty-list