Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按索引位置从稀疏数据帧中选择行_Python_Numpy_Pandas_Scipy_Sparse Matrix - Fatal编程技术网

Python 按索引位置从稀疏数据帧中选择行

Python 按索引位置从稀疏数据帧中选择行,python,numpy,pandas,scipy,sparse-matrix,Python,Numpy,Pandas,Scipy,Sparse Matrix,在典型的python数据框中,可以很容易地根据索引选择所需的行: df.ix[list_of_inds] or df.loc[list_of_inds] 然而,使用这种方法获取大型稀疏数据帧(73000行,特别是8000列)的一个重要子集似乎非常密集——我的内存突然膨胀,计算机崩溃 我确实注意到使用这样的范围进行索引 df.ix[1:N] 工作正常,但使用这样的索引列表 df.ix[np.arange(1,N)] 是什么使内存过载 是否有另一种方法可以从稀疏数据帧中选择计算更简单的行?或者

在典型的python数据框中,可以很容易地根据索引选择所需的行:

df.ix[list_of_inds] or df.loc[list_of_inds]
然而,使用这种方法获取大型稀疏数据帧(73000行,特别是8000列)的一个重要子集似乎非常密集——我的内存突然膨胀,计算机崩溃

我确实注意到使用这样的范围进行索引

df.ix[1:N]
工作正常,但使用这样的索引列表

df.ix[np.arange(1,N)]
是什么使内存过载

是否有另一种方法可以从稀疏数据帧中选择计算更简单的行?或者,我可以将这个数据帧转换成实际的稀疏矩阵

sparse_df = scipy.sparse.csc(df)

并且只从中选择我想要的索引?

您面临的问题可能与查看与复制语义有关

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形状的数据帧,我的内存达到了4.4GB,所以我不会对崩溃感到惊讶。也就是说,如果您确实需要使用索引列表创建一个新数组,那么您就不走运了。但是,要修改原始数据帧,您应该能够一次修改一行数据帧,或者一次修改几个切片行,但以牺牲速度为代价,例如:

for i in arbitrary_list_of_indices:
    df.ix[i] = new_values 
顺便说一句,您可以尝试直接使用numpy阵列,我觉得它可以更清楚地描述哪些操作会导致副本和视图。您总是可以从阵列创建数据帧,几乎不需要任何内存开销,因为它只创建对原始阵列的引用

此外,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

祝你好运

您是否尝试过
稀疏
方法?尝试一下——似乎需要一段时间。to_稀疏方法生成的数据帧可以很容易地进行子集划分吗?编辑:在我的73000x8000数据帧上使用稀疏崩溃我的计算机您是否尝试:
list\u of_inds=pd.Index(list\u of_inds);df.ix[索引列表]