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