跳过空列表继续函数的修改
modification of 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 []
工作代码
以下代码来自
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)
在最后添加这一行fillna
df['New'].fillna(df['Text'],inplace=True)
@tawab_shakeel 接近。只需添加:
df['New'].fillna(df['Text'], inplace=True)
fillna
将从 df['Text']
.
中捕获正确的值
我还可以使用正则表达式的 re 模块提出替代解决方案。
def replacing(x):
if len(x['P_Name']) > 0:
return re.sub('|'.join(x['P_Name']), '**BLOCK**', x['Text'])
else:
return x['Text']
df['New'] = df.apply(replacing, axis=1)
apply
方法对每一行应用replacing
函数,替换由re.sub函数完成。
背景
以下代码由
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 []
工作代码
以下代码来自
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)
在最后添加这一行fillna
df['New'].fillna(df['Text'],inplace=True)
@tawab_shakeel 接近。只需添加:
df['New'].fillna(df['Text'], inplace=True)
fillna
将从 df['Text']
.
我还可以使用正则表达式的 re 模块提出替代解决方案。
def replacing(x):
if len(x['P_Name']) > 0:
return re.sub('|'.join(x['P_Name']), '**BLOCK**', x['Text'])
else:
return x['Text']
df['New'] = df.apply(replacing, axis=1)
apply
方法对每一行应用replacing
函数,替换由re.sub函数完成。