本文介绍了按列对python数组/recarray进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何按给定列对整个数组/重新数组进行排序,我有一个相当简单的问题.例如,给定数组:

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])] 其中 0 是要排序的列索引:

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=data[np.argsort(data[:,col])]
Out[30]:
array([[3, 6],
       [4, 1],
       [5, 2]])

这篇关于按列对python数组/recarray进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:59