modification of skipping empty list and continuing with function

前端 未结 2 1794
失恋的感觉
失恋的感觉 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 18:50

    Just add this line in the end fillna

    df['New'].fillna(df['Text'],inplace=True)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题