【问题标题】:Remove punctuation from a pandas Series从熊猫系列中删除标点符号
【发布时间】:2020-11-15 14:49:27
【问题描述】:

这是我的series,它已被标记并删除了停用词:

0        [laptop, sits, 4, stars, similarly, priced, co...
1        [ordered, monitor, wanted, makeshift, area, po...
2        [monitor, great, deal, price, size, ., use, of...
3        [bought, height, adjustment, ., swivel, abilit...
4        [worked, month, died, ., 5, calls, hp, support...
                               ...                        
30618                                        [great, deal]
30619                                  [pour, le, travail]
30620                                      [business, use]
30621                                         [good, size]
30622    [pour, mon, ordinateur.plus, grande, image.vra...
Name: text_body, Length: 30623, dtype: object

我想从上述系列中删除标点符号。我曾尝试过这样的事情

filtered_text = re.sub(r'[^\w\s]','',str(series))

结果以字符串形式出现。

我有 2 个问题。

  1. 有没有办法将filtered_text 字符串转换回列表或系列?
  2. 有没有更好的方法从原始系列中删除标点符号?

【问题讨论】:

    标签: python pandas nlp data-cleaning


    【解决方案1】:

    您最好从这样的系列中删除punctuations:

    filtered_text = s.str.replace('[^\w\s]','')
    

    s 是您的系列。

    说明:

    您首先通过.str 将您的系列转换为字符串,然后应用replace 正则表达式。 现在您不必再担心将其转换回series。

    【讨论】:

    • 我试过你的方法。似乎filtered_text 结果充满了NaN 值。有什么办法可以解决这个问题吗?无论如何,谢谢你的回答,非常感谢。
    • 你能分享更多result变成NaN的数据吗?如果问题解决了,请您点击我的回答旁边的tick-mark 接受我的回答。
    猜你喜欢
    • 2021-01-11
    • 2020-08-11
    • 2017-02-08
    • 2013-12-12
    • 1970-01-01
    • 2020-12-19
    • 2021-09-25
    • 2017-10-01
    • 2021-09-10
    相关资源
    最近更新 更多