【问题标题】:How to remove one or more letter x from pandas series?如何从熊猫系列中删除一个或多个字母 x?
【发布时间】:2020-10-23 23:30:00
【问题描述】:

我一直在尝试从 pandas 系列中删除仅包含字母 x 的单词,但它没有按预期工作。 如何从熊猫系列中删除像x xx xxx xxxx 这样只能有任意数量x 的词?

我的尝试

import numpy as np
import pandas as pd
pd.set_option('max_colwidth',500)

data = np.array(['transworld system inc trying colect xx xxxx debt mine owed inacurate',
       'complaint cals ocur betwen xx xx xx xx cel job ',
       'company violated',
       'previously xxxx xxxx xxxx requested experian actualy mine xxxx xxxx instead',
       'company xx trans union xx noticed '])
                
                
s = pd.Series(data)
s.str.replace(r'(\sx+\s)',r' ',regex=True)

输出

我期待空格字母*N 空格替换为一个空格,但它不起作用。 如何解决问题?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以使用\b 作为单词边界,所以'xxx abcd' 可以被替换。由于x 可以交错,您可以将它们全部删除,您可以使用:

    s.str.replace(r'\b[x\s]+\b',' ')
    

    输出:

    0    transworld system inc trying colect debt mine ...
    1                  complaint cals ocur betwen cel job 
    2                                     company violated
    3    previously requested experian actualy mine ins...
    4                         company trans union noticed 
    dtype: object
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2013-12-12
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 2020-12-19
      相关资源
      最近更新 更多