Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R's order equivalent in python

Any ideas what is the python's equivalent for R's order?

order(c(10,2,-1, 20), decreasing = F) 
# 3 2 1 4
like image 947
Deena Avatar asked Sep 08 '16 07:09

Deena


2 Answers

In numpy there is a function named argsort

import numpy as np
lst = [10,2,-1,20]
np.argsort(lst)
# array([2, 1, 0, 3]) 

Note that python list index starting at 0 while starting at 1 in R.

like image 167
PhilChang Avatar answered Nov 17 '22 18:11

PhilChang


It is numpy.argsort()

import numpy
a = numpy.array([10,2,-1, 20])
a.argsort()

# array([2, 1, 0, 3])

and if you want to explore the decreasing = T option. You can try,

(-a).argsort()

#array([3, 0, 1, 2])
like image 8
Ronak Shah Avatar answered Nov 17 '22 18:11

Ronak Shah