mardi 12 juillet 2016

Python: argsort in descending order for 2d array?

i need to sort by the first column in descending order an array. To be specific my code is:

>>> x = np.array([[2, 3], [1998,5], [1998,7]])
>>> x = x[np.argsort(x[:,0])]

but the output is

array([[   2,    3],
       [1998,    5],
       [1998,    7]])

but i need it in descending order. Can someone explain me how to do that?

edit : Babyburger suggested this solution :

x = x[np.argsort(x[:,0])][::-1]

that give array([[1998, 7], [1998, 5], [ 2, 3]])

it could be fine, but i would like that where the value on the first column is the same, the order is not changed. So the output would be

array([[1998,    5],
       [1998,    7],
       [   2,    3]])

is there a simple way to do that?

Aucun commentaire:

Enregistrer un commentaire