Background
The following code is slightly modified from skipping empty list and continuing with function
import pandas as pd
Names =
Just add this line in the end fillna
df['New'].fillna(df['Text'],inplace=True)
@tawab_shakeel is close. Just add:
df['New'].fillna(df['Text'], inplace=True)
fillna
will catch the correct value from df['Text']
.
I can also propose an alternative solution using the re module for regex.
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)
The apply
method applies the replacing
function to each row, and substitution is done by the re.sub function.