熊猫内存错误

use*_*827 3 python memory pandas

我有一个包含约 50,000 行和 300 列的 csv 文件。执行以下操作会导致 Pandas (python) 中出现内存错误:

merged_df.stack(0).reset_index(1)
Run Code Online (Sandbox Code Playgroud)

数据框如下所示:

GRID_WISE_MW1   Col0    Col1    Col2 .... Col300
7228260         1444    1819    2042
7228261         1444    1819    2042
Run Code Online (Sandbox Code Playgroud)

我正在使用最新的 pandas (0.13.1),并且行数较少 (~2,000) 的数据帧不会出现该错误

谢谢!

Jef*_*eff 5

所以它占用了我的 64 位 Linux (32GB) 内存,略小于 2GB。

In [5]: def f():
       df = DataFrame(np.random.randn(50000,300))
       df.stack().reset_index(1)


In [6]: %memit f()
maximum of 1: 1791.054688 MB per loop
Run Code Online (Sandbox Code Playgroud)

既然你没有具体说明。这在 32 位上根本不起作用(因为您通常无法分配 2GB 连续块),但如果您有合理的交换/内存,则应该可以工作。

  • 是的; 你可以安装 64 位 python(和所有包),或者使用“conda”来执行此操作。32 位有 4GB 的可寻址限制,但 python 需要连续的内存,因此太大而无法可靠地堆栈。根据我的经验,32 位对于任何大于 1GB 的内容都会出现问题;不过 64 位缩放没有问题。 (2认同)