【问题标题】:pandas: replace only the word and not the entire sentencepandas:只替换单词而不是整个句子
【发布时间】:2022-01-13 09:04:19
【问题描述】:

我有一个如下的数据框:(e,g)

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

我想将以下列表中收集的星期几替换为来自假库中的随机日期,用于每次出现的日子。我做了以下事情:

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

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

我尝试了以下方法,但都返回了替换的日期而不是整个句子:

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

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

我想要的输出:

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

【问题讨论】:

    标签: python pandas string replace contains


    【解决方案1】:

    使用 lambda 函数替换回调:

    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 .
    

    【讨论】:

      【解决方案2】:
      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)
      

      输出:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-24
        • 2019-12-30
        • 2019-06-17
        • 2014-05-18
        • 2022-01-13
        • 2014-11-02
        • 2021-11-23
        相关资源
        最近更新 更多