python - 如何提高熊猫数据框的列表理解速度
问题描述
除了列表理解之外,是否有更快的方法从集合中过滤项目,对于大型数据集,列表理解运行时间有点慢。
我已经将其转换list_stopwords
为一组,与列表相比,这需要更少的时间。
date description
0 2018-07-18 payment receipt
1 2018-07-18 ogsg s.u.b.e.b june 2018 salar
2 2018-07-18 sal admin charge
3 2018-07-19 sms alert charge outstanding
4 2018-07-19 vat onverve*issuance
list_stopwords = set(stop_words.get_stop_words('en'))
data['description'] = data['description'].apply(lambda x: " ".join([word for word in x.split() if word not in (list_stopwords)]))
解决方案
也许使用正则表达式可以更快地工作:
拳头创建您的匹配案例正则表达式:
list_stopwords = set(stop_words.get_stop_words('en'))
re_stopwords= r"\b["
for word in list_stopwords:
re_stopwords+= "("+word+")"
re_stopwords+=r"]\b"
现在,应用于列:
data['description'] = data['description'].apply(lambda x: re.sub(re_stopwords,'',x))
''
这将用(空字符串)替换所有停用词。
我相信它更快,因为正则表达式直接在字符串上操作,而不是你的代码在拆分时得到一个循环。
推荐阅读
- php - Paytm 支付网关在 php 中显示错误
- groovy - 如何在 Groovy (2.5.5) 中元素的“约束”部分添加多个 GridBagLayout 属性?
- javascript - 如何以功能方式有条件地分配给变量?
- javascript - 在 React Styleguidist 文档中不能使用其他组件作为子组件
- python - 如何在 python selenium 中处理 html 标签'message-banner'?
- php - 根据 URL 参数在选择字段中自动选择一个选项?
- c# - ONVIF 配置文件和 VideoSources 之间有什么关系?
- javascript - 为什么 gl.drawElements 需要重新绑定而 gl.drawArrays 不需要?
- linux - 将base64字符串解码为文件中的十六进制字符串并覆盖
- python - 我如何改变口音?