modification of skipping empty list and continuing with function

前端 未结 2 1795
失恋的感觉
失恋的感觉 2021-01-28 18:25

Background

The following code is slightly modified from skipping empty list and continuing with function

import pandas as pd
Names =             


        
2条回答
  •  离开以前
    2021-01-28 19:05

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

提交回复
热议问题