【发布时间】:2019-03-22 12:50:03
【问题描述】:
我必须处理大量(大约 10GB)的 CSV 文件。我目前正在使用 Dask 将数据预处理为一些汇总统计数据,然后我使用常规 Pandas 进一步分析。
我遇到的问题是 Dask 会为每次调用 compute() 重新加载数据。一些虚拟代码来说明问题:
import dask.dataframe as dd
ddf = dd.read_csv('very_large_file.csv') # ca. 10GB
# Every line seems to trigger painfully slow re-reading of the CSV file from disk!
groupstats_A = ddf.groupby(['col1', 'col2']) \
.mean() \
.compute()
groupstats_B = ddf.groupby(['col3']) \
.mean() \
.compute()
groupstats_C = ddf.groupby(['col1', 'col2', 'col3']) \
.mean() \
.compute()
有没有办法优化此代码,使compute() 函数不必在每次调用时都从磁盘读取大文件?
【问题讨论】: