简体   繁体   中英

Remove substring from the words in pandas dataframe

I have the pandas data frame:

df:

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

I want to add a new column "new" to the dataframe, where all the words in the df.des that has the substring tis , ber uti be removed

That is

df["new"]:

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

How do I do this

You can use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM