您面临的问题可能与视图与复制语义有关。
df.ix[1:N] # uses slicing => operates on a view
df.ix[np.arange(1,N)] # uses fancy indexing => "probably" creates a copy first
我在形状为 73000x8000 的机器上创建了一个 DataFrame,我的内存飙升至 4.4 GB,因此我不会对崩溃感到惊讶。也就是说,如果您确实需要使用索引列表创建一个新数组,那么您就不走运了。但是,要修改原始 DataFrame,您应该能够一次修改 DataFrame 一行,或者一次修改几个切片行,但会牺牲速度,例如:
for i in arbitrary_list_of_indices:
df.ix[i] = new_values
顺便说一句,您可以尝试直接处理 numpy 数组,我觉得它更清楚地描述了哪些操作会导致副本与视图。您始终可以从数组创建一个 DataFrame,几乎没有任何内存开销,因为它只是创建对原始数组的引用。
即使没有切片,numpy 中的索引也似乎更快。这是一个简单的测试用例:
In [66]: df
Out[66]:
0 1 2 3
0 3 14 5 1
1 9 19 14 4
2 5 4 5 5
3 13 14 4 7
4 8 12 3 16
5 15 3 17 12
6 11 0 12 0
In [68]: df.ix[[1,3,5]] # fancy index version
Out[68]:
0 1 2 3
1 9 19 14 4
3 13 14 4 7
5 15 3 17 12
In [69]: df.ix[1:5:2] # sliced version of the same
Out[69]:
0 1 2 3
1 9 19 14 4
3 13 14 4 7
5 15 3 17 12
In [71]: %timeit df.ix[[1,3,5]] = -1 # use fancy index version
1000 loops, best of 3: 251 µs per loop
In [72]: %timeit df.ix[1:5:2] = -2 # faster sliced version
10000 loops, best of 3: 157 µs per loop
In [73]: arr = df.values
In [74]: arr
Out[74]:
array([[ 3, 14, 5, 1],
[-2, -2, -2, -2],
[ 5, 4, 5, 5],
[-2, -2, -2, -2],
[ 8, 12, 3, 16],
[-2, -2, -2, -2],
[11, 0, 12, 0]])
In [75]: %timeit arr[[1,3,5]] = -1 # much faster than DataFrame
The slowest run took 23.49 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 4.56 µs per loop
In [77]: %timeit arr[1:5:2] = -3 # really fast but restricted to slicing
The slowest run took 19.46 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 821 ns per loop
祝你好运!