Sorting a python array/recarray by column
Asked Answered
L

5

36

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]])
Lambeth answered 26/7, 2011 at 19:38 Comment(0)
S
55

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]])
Separate answered 26/7, 2011 at 19:48 Comment(2)
How would you adapt this to sort by more than one column (with one column as higher priority)?Aldershot
You can use np.lexsort .Yonina
S
14

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]]
Sailcloth answered 26/7, 2011 at 19:46 Comment(0)
C
6

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

Collapse answered 26/7, 2011 at 20:0 Comment(0)
E
5

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]]
>>> 
Ebersole answered 26/7, 2011 at 19:49 Comment(0)
E
1

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]])
Enwreathe answered 29/4, 2020 at 9:22 Comment(1)
Could I sort all matrix columns in descending order in similar way using argsort()?Mulvihill

© 2022 - 2024 — McMap. All rights reserved.