[英]Python - How to split an array based on the first column?
I have below fake data.我有以下虚假数据。 After reading it into array it will have shape (8, 3).将其读入数组后,它将具有形状 (8, 3)。 Now I want to split the data based on the first column(ID) and return a list of array whose shape will be:[(3,3),(2,3),(3,3)].现在我想根据第一列(ID)拆分数据并返回一个数组列表,其形状为:[(3,3),(2,3),(3,3)]。 I think np.split could do the job by assigning a 1-D array to "indices_or_sections" argument.我认为 np.split 可以通过将一维数组分配给“indices_or_sections”参数来完成这项工作。 But is there any more convenient way to do this?但是有没有更方便的方法来做到这一点?
1 700 35
1 700 35
1 700 35
2 680 25
2 680 25
3 750 40
3 750 40
3 750 40
You can achieve this by using a combination of np.split
, sort
, np.unique
and np.cumsum
.您可以通过使用np.split
、 sort
、 np.unique
和np.cumsum
的组合来实现此目的。
>>> a = [[1, 700, 35],
... [1, 700, 35],
... [1, 700, 35],
... [2, 680, 25],
... [2, 680, 25],
... [3, 750, 40],
... [3, 750, 40],
... [3, 750, 40]]
>>> a = np.array(a)
>>> # sort the array by first column.
>>> a = a[a[:,0].argsort()]
>>> np.split(a, np.cumsum(np.unique(a[:, 0], return_counts=True)[1])[:-1])
[array([[ 1, 700, 35],
[ 1, 700, 35],
[ 1, 700, 35]]), array([[ 2, 680, 25],
[ 2, 680, 25]]), array([[ 3, 750, 40],
[ 3, 750, 40],
[ 3, 750, 40]])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.