如何修复筛选数据帧时的内存错误?



>在对数据帧进行简单筛选时出现内存错误。在我的笔记本电脑上重新安装 Windows 10 之前,以前从未遇到过此问题。

我尝试了无法分配具有形状和数据类型的数组的解决方法,并更改了页面文件大小。此外,我用 32 位 python 重新安装了我的 64 位 python。

我的csv文件不是很大,大约600 MB,我的RAM是16 GB。我的代码:

df = pd.read_csv('file.csv')
quant = df['a'].quantile(0.995)
df = df[df['a']<quant]

最后我得到这个错误MemoryError: Unable to allocate 171. MiB for an array with shape (3, 7483698) and data type float64

此问题的性质是什么以及如何解决?

使用 dask 数据帧可以将数据帧拆分为跨 CPU 内核的较小熊猫数据帧。这将在 dask 合并结果之前启用并行计算。

import dask.dataframe as dd
#Run dask.read_csv() which is quite similar to pandas.read_csv
df = dd.read_csv('file.csv')
#Use — .compute() — to initiate calculation
df = df[ df['a'] < dd['a'].quantile(0.995) ].compute()
#The output from using .compute() is a pandas dataframe
df

源:

  1. https://docs.dask.org/en/latest/dataframe.html

所以基本上答案是正确卸载Python 32位并安装Python 64位。

最新更新