2

I understood that sorting a numpy array arr by column (for only a particular column, for example, its 2nd column) can be done with:

arr[arr[:,1].argsort()]

How I understood this code sample works: argsort sorts the values of the 2nd column of arr, and gives the corresponding indices as an array. This array is given to arr as row numbers. Am I correct in my interpretation?

Now I wonder what if I want to sort the array arr with respect to the 2nd row instead of the 2nd column? Is the simplest way to transpose the array before sorting it and transpose it back after sorting, or is there a way to do it like previously (by giving an array with the number of the columns we wish to display)?

Instead of doing (n,n)array[(n,)array] (n is the size of the 2d array) I tried to do something like (n,n)array[(n,1)array] to indicate the numbers of the columns but it does not work.

EXAMPLE of what I want:

arr = [[11,25],[33,4]] => base array

arr_col2=[[33,4],[11,25]] => array I got with argsort()

arr_row2=[[25,11],[4,33]] => array I tried to got in a simple way with argsort() but did not succeed

benten
  • 1,995
  • 2
  • 23
  • 38
JoVe
  • 435
  • 2
  • 9
  • 17
  • Possible duplicate of [how to sort 2d array by row in python?](http://stackoverflow.com/questions/2173797/how-to-sort-2d-array-by-row-in-python) – Stefano Apr 27 '16 at 11:52
  • I suspect that this is a numpy array, not a vanilla python array - in which case it's not a duplicate of that question (though probably has been asked elsewhere anyway) – Josh Hunt Apr 27 '16 at 12:01
  • It is not a duplicate indeed. What I want is to know if there is a similar way to the method I described to sort a numpy array wrt a particular row, without mixing the columns (instead of sorting wrt to a column). The main thing I dont get is how 2darray[1darray] works. – JoVe Apr 28 '16 at 07:34

1 Answers1

3

I assume that arr is a numpy array? I haven't seen the syntax arr[:,1] in any other context in python. It would be worth mentioning this in your question!

Assuming this is the case, then you should be using

arr.sort(axis=0)

to sort by column and

arr.sort(axis=1)

to sort by row. (Both sort in-place, i.e. change the value of arr. If you don't want this you can copy arr into another variable first, and apply sort to that.)

If you want to sort just a single row (in this case, the second one) then

arr[1,:].sort()

works.

Edit: I now understand what problem you are trying to solve. You would like to reorder the columns in the matrix so that the nth row goes in increasing order. You can do this simply by

arr[:,arr[1,:].argsort()]

(where here we're sorting by the 2nd row).

Josh Hunt
  • 620
  • 7
  • 10
  • You assumed correctly =). However I tried to sort the way you are telling before using the method I described (seemed more logical...), but it sorts all the rows or all the columns, whereas I want to only sort one particular column whithout mixing the rows. And I wanted to know if there was a way to sort one particular row the same way with argsort – JoVe Apr 28 '16 at 07:26
  • I've updated my answer to answer that - again I still don't think argsort is the way to go! – Josh Hunt Apr 28 '16 at 11:50
  • Then again it is not what I want, since I want the rows to remain the same. By doing `arr[1,:].sort()` the column 2 is sorted but the column 1 is not, which causes the rows to be mixed up compared to the unsorted array. Do you see what I mean ? – JoVe Apr 28 '16 at 15:55
  • Sorry, could you give me an example of a (say) 3x3 matrix as input and the output you're looking for? – Josh Hunt Apr 28 '16 at 16:47
  • Ok I should have begun by this. I edited the first post with an example – JoVe Apr 29 '16 at 08:31
  • Exactly the answer I was looking for, thanks ! But for your edit to be correct it should be `arr[:,arr[1,:].argsort()]` instead of `arr[:,arr[:,1].argsort()]`, I think – JoVe Apr 29 '16 at 14:51