按列对 python 数组/recarray 进行排序

发布于 2024-11-26 08:32:49 字数 216 浏览 2 评论 0原文

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

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

我想按要返回的第一列对数据进行排序:

array([[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]])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回心转意 2024-12-03 08:32:49

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

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]])

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]])
将军与妓 2024-12-03 08:32:49

您正在寻找 operator.itemgetter

>>> from operator import itemgetter, attrgetter

>>> sorted(student_tuples, key=itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

>>> sorted(student_objects, key=attrgetter('age'))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

In [7]: a
Out[7]: [[5, 2], [4, 1], [3, 6]]

In [8]: sorted(a, key=operator.itemgetter(0))
Out[8]: [[3, 6], [4, 1], [5, 2]]

you are looking for operator.itemgetter

>>> from operator import itemgetter, attrgetter

>>> sorted(student_tuples, key=itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

>>> sorted(student_objects, key=attrgetter('age'))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

i.e.

In [7]: a
Out[7]: [[5, 2], [4, 1], [3, 6]]

In [8]: sorted(a, key=operator.itemgetter(0))
Out[8]: [[3, 6], [4, 1], [5, 2]]
岁月蹉跎了容颜 2024-12-03 08:32:49

这有点棘手:

data[data[:,0].argsort()]

# data[:,n] -- get entire column of index n
# argsort() -- get the indices that would sort it
# data[data[:,n].argsort()] -- get data array sorted by n-th column

我在这里找到了这个食谱:

链接

http://mathesaurus.sourceforge.net/matlab-numpy.html

This is somewhat tricky:

data[data[:,0].argsort()]

# data[:,n] -- get entire column of index n
# argsort() -- get the indices that would sort it
# data[data[:,n].argsort()] -- get data array sorted by n-th column

I found this recipe here:

Link

http://mathesaurus.sourceforge.net/matlab-numpy.html

狼性发作 2024-12-03 08:32:49

要对第二列进行排序,请使用 itemgetter

>>> from operator import itemgetter
>>> data = [[5,2], [4,1], [3,6]]
>>> sorted(data)
[[3, 6], [4, 1], [5, 2]]
>>> sorted(data,key=itemgetter(1))
[[4, 1], [5, 2], [3, 6]]
>>> 

To sort on the second column use itemgetter

>>> from operator import itemgetter
>>> data = [[5,2], [4,1], [3,6]]
>>> sorted(data)
[[3, 6], [4, 1], [5, 2]]
>>> sorted(data,key=itemgetter(1))
[[4, 1], [5, 2], [3, 6]]
>>> 
苍白女子 2024-12-03 08:32:49

这是一个适用于切片的扩展:

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

按行排序:

x[:, x[1,:].argsort()] # Sort by second row

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

按列排序:

x[x[:,0].argsort(), :] # Sort by first column

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

Here's an extension that works with slices:

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

Sorting by rows:

x[:, x[1,:].argsort()] # Sort by second row

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

Sorting by columns:

x[x[:,0].argsort(), :] # Sort by first column

array([[0, 5, 6],
       [5, 3, 4],
       [9, 1, 2]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文