Home > other >  pandas: replace only the word and not the entire sentence
pandas: replace only the word and not the entire sentence

Time:12-08

I have a dataframe as follows: (e,g)

import pandas as pd
df = pd.DataFrame({'text':['Lary Page is visiting on Saturday',' On Monday his boss, Maria Jackson is here .']})

I would like to replace days of the week gathered in the following list with a random day from the fake library for each occurrence of days. I have done the following:

from faker import Faker
import numpy as np
fake = Faker()

days_list = ['Saturday','Monday','Tuesday']

I have tried the following, but both return the replaced day and not the entire sentence:

df.text = np.where(df.text.str.contains('|'.join(days_list)),
               fake.day_of_week(), df.text)

or

df.text.str.replace('|'.join(days_list), fake.day_of_week())

my desired output:

print(df): (e,g)
'Lary Page is visiting on Tuesday'
'On Thursday his boss, Maria Jackson is here .'

CodePudding user response:

Use lambda function for replace with callback:

regex = '|'.join(days_list)
df['text'] = df.text.str.replace(regex, lambda x: fake.day_of_week(), regex=True)
print (df)
                                             text
0                Lary Page is visiting on Tuesday
1   On Thursday his boss, Maria Jackson is here .

CodePudding user response:

from faker import Faker
import pandas as pd
df = pd.DataFrame({'text':['Lary Page is visiting on Saturday',' On Monday his boss, Maria Jackson is here .']})

fake = Faker()
days_list = ['Saturday','Monday','Tuesday']
df['text'] = df['text'].apply(lambda x: ' '.join(fake.day_of_week() if i in days_list else i for i in x.split()))

print(df)

output:

                                          text
0             Lary Page is visiting on Tuesday
1  On Monday his boss, Maria Jackson is here .
  • Related