Skip to content Skip to sidebar Skip to footer

Modification Of Skipping Empty List And Continuing With Function

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']),

Solution 1:

Just add this line in the end fillna

df['New'].fillna(df['Text'],inplace=True)

Solution 2:

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

defreplacing(x):
    iflen(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.

Post a Comment for "Modification Of Skipping Empty List And Continuing With Function"