-2

m,n) shaped numpy array. I would like to sort the whole array by the values in the index [1,2,:] say. Is there an easy way to do this? I tried to use pandas Panel but for whatever reason there is no sort by values function for it.

That 'duplicate' someone linked only seems to work for a column in an 2d array. I am using the term column in a more general sense i.e a 1d sub-array of an (mxnxlx..) array

shaun252
  • 61
  • 4
  • 1
    Possible duplicate of [How to sort multidimensional array by column?](http://stackoverflow.com/questions/20183069/how-to-sort-multidimensional-array-by-column) – jHilscher Aug 22 '16 at 18:05
  • Use `argsort` on the selected row/column, and then order the array with that index. – hpaulj Aug 22 '16 at 18:39

1 Answers1

0

So how I did it was using argsort as hpaulj said, given a array arr that was (m,n,l) dimension and a column indexed by (i,j,:).

sorted = np.empty(arr.shape):
for k in range(arr.shape[2]):
    sorted[:,:,i] = arr[:,:,np.argsort(arr[i,j,:])[i]]
shaun252
  • 61
  • 4