Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use NumPy to sort a multidimensional array from highest to lowest value

Not sure I’m finding my exact use case in the NumPy documentation, so hoping for help.

I have this array:

X = np.array([
            [Larry, 90%],
            [Beth, 100%],
            [Arnold, 90%],
])

And I’m trying to sort these horizontal pairs by the second index (i.e., the percentage) from highest to lowest value so that the result is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            ([
            [Beth, 100%],
            [Arnold, 90%],
            [Larry, 90%],
])

I tried using argsort, but the expression below didn’t work:

X = X[-X[:, 0].argsort()]

>Solution :

import numpy as np
X = np.array([['Larry', '90%'], ['Beth', '100%'], ['Arnold', '90%']])
X[np.array([-float(num.strip('%'))/100 for num in X[:, 1]]).argsort()]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading