通过排序列蟒蛇阵列/ recarray [英] Sorting a python array/recarray by column

查看:115
本文介绍了通过排序列蟒蛇阵列/ recarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何由给定列整个数组/ recarray排序一个相当简单的问题。例如,给定阵列

 导入numpy的是NP
数据= np.array([5,2],[4,1],[3,6]])

我想用数据的第一列返回排序:

 阵列([3,6],[4,1],[5,2]])


解决方案

使用数据[np.argsort(数据[:,0])] 其中 0 是排序的列索引:

 在[27]:进口numpy的为NP在[28]:数据= np.array([5,2],[4,1],[3,6]])在[29]:COL = 0在[30]:数据[np.argsort(数据[:,COL])]
出[30]:
阵列([[3,6]
       [4,1]
       [5,2]])

I have a fairly simple question about how to sort an entire array/recarray by a given column. For example, given the array:

import numpy as np
data = np.array([[5,2], [4,1], [3,6]])

I would like to sort data by the first column to return:

array([[3,6], [4,1], [5,2]])

解决方案

Use data[np.argsort(data[:, 0])] where the 0 is the column index on which to sort:

In [27]: import numpy as np

In [28]: data = np.array([[5,2], [4,1], [3,6]])

In [29]: col = 0

In [30]: data[np.argsort(data[:,col])]
Out[30]: 
array([[3, 6],
       [4, 1],
       [5, 2]])

这篇关于通过排序列蟒蛇阵列/ recarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆