I want to get the rank of each element, so I use argsort in numpy:
argsort
numpy
np.argsort(np.array((1,1,1,2,2,3,3,3,3))) array([0, 1, 2, 3, 4, 5, 6
Alternatively, pandas series has a rank method which does what you need with the min method:
rank
min
import pandas as pd pd.Series((1,1,1,2,2,3,3,3,3)).rank(method="min") # 0 1 # 1 1 # 2 1 # 3 4 # 4 4 # 5 6 # 6 6 # 7 6 # 8 6 # dtype: float64