首页 > 解决方案 > 使用多处理在 python 中读取多个大型 csv 文件的最佳策略?

问题描述

我正在编写一些代码,并希望通过多处理来改进它。

最初,我有以下代码:

with Pool() as p:
        lst = p.map(self._path_to_df, paths)
...
df = pd.concat(lst, ignore_index=True)

self._path_to_df()基本上只是调用并pandas.read_csv(...)返回一个 pandas DataFrame。

这会导致以下错误:

.
.
.
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 268, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 657, in get
    raise self._value
multiprocessing.pool.MaybeEncodingError: Error sending result: '[                    ts                  id.orig  ...  successful history_category
0         1.331901e+09               ...        True            other
1         1.331901e+09               ...        True                ^
2         1.331901e+09               ...        True               Sh
3         1.331901e+09               ...        True               Sh
4         1.331901e+09               ...        True               Sh
...                ...               ...         ...              ...
23192090  1.332018e+09               ...       False            other
23192091  1.332017e+09               ...        True            other
23192092  1.332018e+09               ...        True            other
23192093  1.332018e+09               ...        True            other
23192094  1.332018e+09               ...        True            other

[23192095 rows x 24 columns]]'. Reason: 'error("'i' format requires -2147483648 <= number <= 2147483647")'

该错误来自它正在读取的文件之一太大而self._path_to_df()无法在使用多处理时返回 DataFrame。

可能涉及多个不同大小的文件(从小到大 3GB+),所以我试图找出在这项任务中使用多处理的最佳方法是什么。

我应该以某种方式将所有数据分块以便p.map()可以工作还是开销太大?如果是这样,我该怎么做?我应该在读取每个文件时使用多处理并按顺序查看每个文件吗?

编辑:此外,当它只涉及较小的文件时,它似乎不会出错

标签: pythonpandasmultiprocessing

解决方案


如果最终结果太大而无法放入内存,请尝试 dask,

import dask.dataframe as dd
df = dd.read_csv('*.csv')

然后一旦读取,您就可以进行聚合等,最后计算以获得您想要的答案。


推荐阅读