【发布时间】:2020-04-30 22:41:09
【问题描述】:
我在一年的时间跨度内记录了传感器数据。数据存储在 12 个块中,有 1000 列,每列约 1000000 行。我已经制定了一个脚本来将这些块连接到一个大文件,但是在执行大约一半的时候我得到了一个MemoryError。 (我在一台有大约 70 GB 可用 RAM 的机器上运行它。)
import gc
from os import listdir
import pandas as pd
path = "/slices02/hdf/"
slices = listdir(path)
res = pd.DataFrame()
for sl in slices:
temp = pd.read_hdf(path + f"{sl}")
res = pd.concat([res, temp], sort=False, axis=1)
del temp
gc.collect()
res.fillna(method="ffill", inplace=True)
res.to_hdf(path + "sensor_data_cpl.hdf", "online", mode="w")
我也尝试过使用HDFStore,因此我不必将所有数据加载到内存中(请参阅Merging two tables with millions of rows in Python),但我无法弄清楚在我的情况下它是如何工作的。
【问题讨论】:
-
据我所知,pandas 在处理这种大量数据方面并不是很强大 - 不是从我自己的经验或知识来看,尽管快速搜索提出了这个提示,例如 @ 987654322@
-
我同意尤里的观点。
pandas不适合这种数据量。您可能希望转向使用parquet格式文件的pyspark等解决方案。