【发布时间】:2018-10-30 15:44:54
【问题描述】:
我有一个大约 100M 行的 pandas 数据框。并行处理在多核机器上运行良好,每个核的利用率为 100%。但是,executor.map() 的结果是一个生成器,因此为了实际收集处理后的结果,我遍历该生成器。这非常非常慢(几个小时),部分原因是它是单核,部分原因是循环。其实比my_function()中实际处理要慢很多
有没有更好的方法(可能是并发的和/或矢量化的)?
编辑:在 Python 3.7.0 中使用 pandas 0.23.4(目前最新)
import concurrent
import pandas as pd
df = pd.DataFrame({'col1': [], 'col2': [], 'col3': []})
with concurrent.futures.ProcessPoolExecutor() as executor:
gen = executor.map(my_function, list_of_values, chunksize=1000)
# the following is single-threaded and also very slow
for x in gen:
df = pd.concat([df, x]) # anything better than doing this?
return df
【问题讨论】:
-
我认为只有
pd.concat(gen)会起作用并且应该更快,尽管总体上这可能仍然相当慢。 -
我试过了(请参阅 Sraw 答案的评论),但不支持。
-
你为什么还在对
concat的调用中加入df。它应该只是df = pd.concat(gen) -
哦,我们将这些附加到现有的 df。你是对的,这里不需要这个问题,我现在就试试。
-
感谢@ALollz,效果很好。
标签: python pandas concurrency parallel-processing