从 pandas 系列中删除标点符号
Remove punctuation from a pandas Series
这是我的 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 个问题。
- 有没有办法将
filtered_text
字符串转换回列表或系列?
- 是否有更好的方法从原始系列中删除标点符号?
理想情况下,您应该从这样的系列中删除 punctuations
:
filtered_text = s.str.replace('[^\w\s]','')
其中 s
是您的系列。
解释:
您首先通过 .str
将系列转换为字符串,然后应用 replace
正则表达式。
现在您不必担心再次将其转换回 series
。
这是我的 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 个问题。
- 有没有办法将
filtered_text
字符串转换回列表或系列? - 是否有更好的方法从原始系列中删除标点符号?
理想情况下,您应该从这样的系列中删除 punctuations
:
filtered_text = s.str.replace('[^\w\s]','')
其中 s
是您的系列。
解释:
您首先通过 .str
将系列转换为字符串,然后应用 replace
正则表达式。
现在您不必担心再次将其转换回 series
。