modification of skipping empty list and continuing with function

只愿长相守 提交于 2019-12-02 10:18:14
Valentino

@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.

Just add this line in the end fillna

df['New'].fillna(df['Text'],inplace=True)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!