Skip to content Skip to sidebar Skip to footer

Numpy Argsort, Sorting On First Number

I've got the following code: import numpy as np a = np.array([['value1', 'value2', 3, 'value4', 'value5'], ['value1', 'value2', -10, 'value4', 'value5'],

Solution 1:

As mentioned in the comments, the values you are sorting are strings. Change

a = a[a[:, 2].argsort()]

to

a = a[a[:, 2].astype(np.int).argsort()]

So that they are compared as integers.

Solution 2:

You must convert the third column to integer.

Look at here:

a = ['5']
b = ['31']
if a[0] > b[0]:
    print("{} is bigger than {}".format(a[0], b[0]))

Out[0]: 5is bigger than 31

a = int(a[0])
b = int(b[0])
if b > a:
    print("{} is bigger than {}".format(b, a)) 

Out[1]: 31is bigger than 5

For this reason, follow this:

import numpy as npa= np.array('your array')
 a = a[a[:, 2].astype(np.int).argsort()]

Post a Comment for "Numpy Argsort, Sorting On First Number"