【问题标题】:Remove substring from the words in pandas dataframe从熊猫数据框中的单词中删除子字符串
【发布时间】:2018-10-25 09:35:38
【问题描述】:

我有熊猫数据框:

df:

id  des
1   POS Transfer atis mcdon uber
2   MKLI QC Montreal abelutixy
3   PC - PAYMENT FROM - *****11*22

我想在数据框中添加一个“新”列,其中 df.des 中具有子字符串 tisber uti 的所有单词都将被删除

那是

df[“新”]:

   POS Transfer mcdon
   MKLI QC Montreal
   PC - PAYMENT FROM - *****11*22

我该怎么做

【问题讨论】:

    标签: python regex string pandas substring


    【解决方案1】:

    你可以使用:

    In [68]: ddf['new'] = ddf.des.str.replace(r'\w*(tis|ber|uti)\w* ?', '')
    
    In [69]: ddf
    Out[69]: 
                                   des                             new
    id                                                                
    1     POS Transfer atis mcdon uber             POS Transfer mcdon 
    2       MKLI QC Montreal abelutixy               MKLI QC Montreal 
    3   PC - PAYMENT FROM - *****11*22  PC - PAYMENT FROM - *****11*22
    

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 1970-01-01
      • 2019-03-06
      • 2019-03-14
      • 2018-03-23
      • 2021-08-12
      • 2019-01-28
      • 2017-07-27
      • 2021-08-27
      相关资源
      最近更新 更多