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.

1 Answer 1

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.]])
Sign up to request clarification or add additional context in comments.

3 Comments

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 Because you are slicing the first dimension with :, so the order applys to all rows. regardless of first row or second row.
Wow, thanks. Couldn't have asked for a better answer! Worked for me running Python 3.5.3 and numpy 1.13.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.