Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sortrows with multiple sorting keys in numpy

I haven't found this answer on SO, so I am sharing it here:

Question: How do you emulate sortrows functionality in matlab when there are multiple sorting keys? In matlab, this looks like e.g.:

sortrows(x,[3,-4])

which sorts first by the 3rd column and then by the second column.

If you were sorting by one column, you could use np.argsort to find the indices of that column, and apply those indices. But how do you do it for multiple columns?

like image 592
aestrivex Avatar asked Sep 20 '13 15:09

aestrivex


1 Answers

The syntax is quite unwieldy and looks weird, but the cleanest thing to do is np.lexsort.

data = np.array([[3, 0, 0, .24],
                 [4, 1, 1, .41],
                 [2, 1, 1, .63],
                 [1, 1, 3, .38]]) #imagine rows of a spreadsheet
#now do sortrows(data,[3,-4])
ix = np.lexsort((data[:, 3][::-1], data[:, 2])) 
#this yields [0, 2, 1, 3]

#note that lexsort sorts first from the last row, so sort keys are in reverse order

data[ix]
like image 199
aestrivex Avatar answered Oct 10 '22 05:10

aestrivex