【发布时间】:2021-03-06 12:34:03
【问题描述】:
我有一个 10M 记录的数据集,其中第一步是清理数据并使数据集中的单词长度小于 400(如果有)。可以在不使用 numba /dask 或其他多处理库的情况下以原始形式更快地完成这项工作吗?
from cleantext import clean
def func_vect(val):
temp=clean(val,no_line_breaks=True,no_urls=True,no_emails=True,lower=True).split()
if len(temp)<=400:
return " ".join(u for u in temp if len(u)<=15)
else:
return " ".join(u for u in temp[:175]+temp[-175:] if len(u)<=15)
ufunc_vec=np.vectorize(func_vect,otypes=[str])
【问题讨论】:
-
为什么不使用 np.select(condition, choice) 作为 if else 条件。它应该加快速度
-
能举个例子吗?我尝试了 np.select 但在 else 条件下我将不得不将文本拆分两次,对吗?
-
很难在没有数据的情况下为您提供代码。但我们的想法是丢失循环并使用 numpy 对其进行优化。
-
请勿使用
np.vectorize以加快代码速度。在pandas中,字符串值作为 Python 字符串存储在对象 dtype Series 中。pandas确实有将字符串方法应用于系列的方法。numpy不会对字符串做任何快速或花哨的事情。
标签: python python-3.x pandas numpy