Can numpy's argsort give equal element the same rank?

前端 未结 4 1059
半阙折子戏
半阙折子戏 2021-02-07 12:04

I want to get the rank of each element, so I use argsort in numpy:

np.argsort(np.array((1,1,1,2,2,3,3,3,3)))
array([0, 1, 2, 3, 4, 5, 6         


        
4条回答
  •  粉色の甜心
    2021-02-07 12:51

    Alternatively, pandas series has a rank method which does what you need with the min method:

    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
    

提交回复
热议问题