背景
ts-node --project tsconfig.json ./examples/yadda/.ts
目标
我想使用以下功能
import pandas as pd
Names = [list(['Jon', 'Smith', 'jon', 'John']),
list([]),
list(['Bob', 'bobby', 'Bobs'])]
df = pd.DataFrame({'Text' : ['Jon J Smith is Here and jon John from ',
'',
'I like Bob and bobby and also Bobs diner '],
'P_ID': [1,2,3],
'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 2 []
2 I like Bob and bobby and also Bobs diner 3 [Bob, bobby, Bobs]
但是跳过第2行,因为它有一个空列表df['new']=df.Text.replace(df.P_Name,'**PHI**',regex=True)
尝试
我尝试了以下
[]
但是我得到以下输出
try:
df['new']=df.Text.replace(df.P_Name,'**PHI**',regex=True)
except ValueError:
pass
所需的输出
Text P_ID P_Name
0 Jon J Smith is Here and jon John from 1 [Jon, Smith, jon, John]
1 2 []
2 I like Bob and bobby and also Bobs diner 3 [Bob, bobby, Bobs]
问题
如何通过跳过第2行并继续执行功能来获得所需的输出?
答案 0 :(得分:1)
找到没有空列表的行,并仅在这些行上使用replace
方法:
# Boolean indexing the rows which do not have an empty list
m = df['P_Name'].str.len().ne(0)
df.loc[m, 'New'] = df.loc[m, 'Text'].replace(df.loc[m].P_Name,'**PHI**',regex=True)
输出
Text P_ID P_Name New
0 Jon J Smith is Here and jon John from 1 [Jon, Smith, jon, John] **PHI** J **PHI** is Here and **PHI** **PHI** from
1 Test 2 [] NaN
2 I like Bob and bobby and also Bobs diner 3 [Bob, bobby, Bobs] I like **PHI** and **PHI** and also **PHI**s diner