Background
The following code is slightly modified from 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 []
Working code
The following bit of code works taken from 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)
And produces the following New
column in df
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
Desired Output
However, instead of NaN
in row 1
, 3
, 4
, I would like to keep the original text e.g. get nothing from here
as seen below
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
Question
How do I tweak the code below to achieve my desired output?
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)
Just add this line in the end
fillna
@tawab_shakeel is close. Just add:
fillna
will catch the correct value fromdf['Text']
.I can also propose an alternative solution using the re module for regex.
The
apply
method applies thereplacing
function to each row, and substitution is done by the re.sub function.