0

How do I make this numpy array:

[[   0.    1.    2.]
 [ 192.  312.  98.]]

Get sorted into this:

[[   1.    0.    2.]  # Moves entire column instead of just the value in the second row
 [ 312.  192.  98.]]  # Highest to lowest

Thank you.

Elliot Killick
  • 389
  • 2
  • 4
  • 12

1 Answers1

3

Use argsort on the second row and then use the output indices to reorder columns:

a[:, a[1].argsort()[::-1]]
#array([[   1.,    0.,    2.],
#       [ 312.,  192.,   98.]])
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • I have a question. when you a[:, [1,0,2]], [1,0,2] helps you order the second row. But I do not understand why the first row will change according to the second row' order..... – Mr_U4913 Jul 20 '17 at 20:46
  • @Mr_U4913 Because you are slicing the first dimension with `:`, so the order applys to all rows. regardless of first row or second row. – Psidom Jul 20 '17 at 20:47
  • Wow, thanks. Couldn't have asked for a better answer! Worked for me running Python 3.5.3 and numpy 1.13.0. – Elliot Killick Jul 20 '17 at 22:09