【发布时间】:2018-02-11 21:41:08
【问题描述】:
我有一个巨大的.csv 文件(~2GB),我用read_csv 在我的程序中导入它,然后用as_matrix 转换为一个numpy 矩阵。生成的矩阵的形式类似于下面给出的示例中的data_mat。我现在的问题是,我需要提取具有相同 uuid4 的块(矩阵第一列中的条目)。然后由另一个函数处理子矩阵。看来我下面的例子并不是最好的方法。欢迎使用更快的方法。
import numpy as np
data_mat = np.array([['f9f1dc71-9457-4d17-b5d1-e63b5a766f84', 4, 3, 1],\
['f9f1dc71-9457-4d17-b5d1-e63b5a766f84', 3, 1, 1],\
['f9f1dc71-9457-4d17-b5d1-e63b5a766f84', 3, 3, 1],\
['f9f1dc71-9457-4d17-b5d1-e63b5a766f84', 6, 1, 1],\
['f35fb25b-dddc-458a-9f71-0a9c2c202719', 3, 4, 1],\
['f35fb25b-dddc-458a-9f71-0a9c2c202719', 3, 1, 1],\
['a4cf92fc-0624-4a00-97f6-0d21547e3183', 3, 2, 1],\
['a4cf92fc-0624-4a00-97f6-0d21547e3183', 3, 9, 0],\
['a4cf92fc-0624-4a00-97f6-0d21547e3183', 3, 1, 0],\
['a4cf92fc-0624-4a00-97f6-0d21547e3183', 5, 1, 1],\
['a4cf92fc-0624-4a00-97f6-0d21547e3183', 3, 1, 1],\
['d3a8a9d0-4380-42e3-b35f-733a9f9770da', 3, 6, 10]],dtype=object)
unique_ids, indices = np.unique(data_mat[:,0],return_index=True,axis=None)
length = len(data_mat)
i=0
for idd in unique_ids:
index = indices[i]
k=0
while ((index+k)<length and idd == data_mat[index+k,0]):
k+=1
tmp_mat=data_mat[index:(index+k),:]
# do something with tmp_mat ...
print(tmp_mat)
i+=1
【问题讨论】:
-
唯一 ID 是否总是像这样分组在一起(即按
data_mat[:,0]或其他方式排序)? -
是的,唯一的 id 总是像这样组合在一起。我应该提到..
标签: python python-3.x numpy submatrix