0

Background

The following is a minor change from modification of skipping empty list and continuing with function

import pandas as pd
Names =    [list(['ann']),
               list([]),
               list(['elisabeth', 'lis']),
               list(['his','he']),
               list([])]
df = pd.DataFrame({'Text' : ['ann had an anniversery today', 
                                       'nothing here', 
                                       'I like elisabeth and lis 5 lists ',
                                        'one day he and his cheated',
                                        '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   ann had an anniversery today        1   [ann]
1   nothing here                        2   []
2   I like elisabeth and lis 5 lists    3   [elisabeth, lis]
3   one day he and his cheated          4   [his, he]
4   same here                           5   []

The code below works

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 does the following

1) uses the name in P_Name to block the corresponding text in the Text column by placing **BLOCK**

2) produces a new column New

This is shown below

   Text  P_ID P_Name  New
0                     **BLOCK** had an **BLOCK**iversery today
1                     NaN
2                     I like **BLOCK** and **BLOCK** 5 **BLOCK**ts
3                     one day **BLOCK** and **BLOCK** c**BLOCK**ated
4                     NaN

Problem

However, this code works a little "too well."

Using ['his','he'] from P_Name to block Text:

Example: one day he and his cheated becomes one day **BLOCK** and **BLOCK** c**BLOCK**ated

Desired: one day he and his cheated becomes one day **BLOCK** and **BLOCK** cheated

In this example, I would like cheated to stay as cheated and not become c**BLOCK**ated

Desired Output

    Text P_ID P_Name  New
0                     **BLOCK** had an anniversery today
1                     NaN
2                     I like **BLOCK** and **BLOCK**5 lists
3                     one day **BLOCK** and **BLOCK** cheated
4                     NaN

Question

How do I achieve my desired output?

SFC
  • 733
  • 2
  • 11
  • 22

2 Answers2

2

You need to add word boundary to each string in lists of df.loc[m].P_Name as follows:

s = df.loc[m].P_Name.map(lambda x: [r'\b'+item+r'\b' for item in x])

Out[71]:
0                   [\bann\b]
2    [\belisabeth\b, \blis\b]
3           [\bhis\b, \bhe\b]
Name: P_Name, dtype: object

df.loc[m, 'Text'].replace(s, '**BLOCK**',regex=True)

Out[72]:
0       **BLOCK** had an anniversery today
2    I like **BLOCK** and **BLOCK** 5 lists
3    one day **BLOCK** and **BLOCK** cheated
Name: Text, dtype: object
SFC
  • 733
  • 2
  • 11
  • 22
Andy L.
  • 24,909
  • 4
  • 17
  • 29
1

Sometime for loop is good practice

df['New']=[pd.Series(x).replace(dict.fromkeys(y,'**BLOCK**') ).str.cat(sep=' ')for x , y in zip(df.Text.str.split(),df.P_Name)]
df.New.where(df.P_Name.astype(bool),inplace=True)
df
                                Text  ...                                  New
0       ann had an anniversery today  ...     **BLOCK** had an anniversery today
1                       nothing here  ...                                  NaN
2  I like elisabeth and lis 5 lists   ...   I like **BLOCK** and **BLOCK** 5 lists
3         one day he and his cheated  ...  one day **BLOCK** and **BLOCK** cheated
4                          same here  ...                                  NaN
[5 rows x 4 columns]
SFC
  • 733
  • 2
  • 11
  • 22
BENY
  • 317,841
  • 20
  • 164
  • 234