从熊猫数据框中的单词中删除子字符串

Posted

技术标签:

【中文标题】从熊猫数据框中的单词中删除子字符串【英文标题】: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

我该怎么做

【问题讨论】:

【参考方案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

【讨论】:

以上是关于从熊猫数据框中的单词中删除子字符串的主要内容,如果未能解决你的问题,请参考以下文章