modification of skipping empty list and continuing

2020-05-01 04:53发布

问题:

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']),
               list([]),
               list(['Bob', 'bobby', 'Bobs']),
               list([]),
               list([])]
df = pd.DataFrame({'Text' : ['Jon J Smith is Here and jon John from ', 
                                       'get nothing from here', 
                                       'I like Bob and bobby and also Bobs diner ',
                                        'nothing here too',
                                        'same here'
                            ], 

                          'P_ID': [1,2,3, 4,5], 
                          'P_Name' : Names

                         })

    #rearrange columns
df = df[['Text', 'P_ID', 'P_Name']]
df

                                 Text         P_ID  P_Name
0   Jon J Smith is Here and jon John from       1   [Jon, Smith, jon, John]
1   get nothing from here                       2   []
2   I like Bob and bobby and also Bobs diner    3   [Bob, bobby, Bobs]
3   nothing here too                            4   []
4   same here                                   5   []

Working code

The following bit of code works taken from skipping empty list and continuing with function

m = df['P_Name'].str.len().ne(0)
df.loc[m, 'New'] = df.loc[m, 'Text'].replace(df.loc[m].P_Name,'**BLOCK**',regex=True) 

And produces the following New column in df

            Text   P_ID  P_Name   New
0                                 **BLOCK** J **BLOCK** is Here and **BLOCK** **BLOCK** ...
1                                 NaN
2                                 I like **BLOCK** and **BLOCK** and also **BLOCK** d..
3                                 NaN 
4                                 NaN

Desired Output

However, instead of NaN in row 1, 3, 4, I would like to keep the original text e.g. get nothing from here as seen below

            Text   P_ID  P_Name   New
0                                 **BLOCK** J **BLOCK** is Here and **BLOCK** **BLOCK** ...
1                                 get nothing from here
2                                 I like **BLOCK** and **BLOCK** and also **BLOCK** d..
3                                 nothing here too 
4                                 same here

Question

How do I tweak the code below to achieve my desired output?

m = df['P_Name'].str.len().ne(0)
df.loc[m, 'New'] = df.loc[m, 'Text'].replace(df.loc[m].P_Name,'**BLOCK**',regex=True)  

回答1:

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



回答2:

Just add this line in the end fillna

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