现在,我有一个巨大的数据帧"all_in_one",
all_in_one.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8271066 entries, 0 to 8271065
Data columns (total 3 columns):
label int64
text object
type int64
dtypes: int64(2), object(1)
memory usage: 189.3+ MB
all_in_one.sample(2)

我需要在这个数据帧的"text“列上运行分词。
import jieba
import re
def jieba_cut(text):
text_cut = list(filter(lambda x: re.match("\w", x),
jieba.cut(text)))
return text_cut
%%time
all_in_one['seg_text'] = all_in_one.apply(lambda x:jieba_cut(x['text']),axis = 1)
CPU times: user 1h 18min 14s, sys: 55.3 s, total: 1h 19min 10s
Wall time: 1h 19min 10s此过程总共消耗了超过1个小时。我希望在数据帧上并行执行分词,并减少运行时间。请留言。
编辑:
当我使用dask来实现上面的功能时,这真是太神奇了。
all_in_one_dd = dd.from_pandas(all_in_one, npartitions=10)
%%time
all_in_one_dd.head()
CPU times: user 4min 10s, sys: 2.98 s, total: 4min 13s
Wall time: 4min 13s发布于 2019-07-04 11:35:13
我建议,如果您正在使用pandas,并且想要进行某种形式的并行处理,那么我邀请您使用dask。它是一个Python包,具有与pandas数据帧相同的API,因此在您的示例中,如果您有一个名为file.csv的csv文件,您可以执行以下操作:
您必须为dask客户端做一些设置,并告诉它您想要多少个工作进程以及要使用多少个内核。
import dask.dataframe as dd
from dask.distributed import Client
import jieba
def jieba_cut(text):
text_cut = list(filter(lambda x: re.match("\w", x),
jieba.cut(text)))
return text_cut
client = Client() # by default, it creates the same no. of workers as cores on your local machine
all_in_one = dd.read_csv('file.csv') # This has almost the same kwargs as a pandas.read_csv
all_in_one = all_in_one.apply(jieba_cut) # This will create a process map
all_in_one = all_in_one.compute() # This will execute all the processes有趣的是,你实际上可以访问一个仪表板来查看dask完成的所有进程(我认为默认情况下它是localhost:8787)
https://stackoverflow.com/questions/56880100
复制相似问题