Strength increment in pandas ranking method

I am evaluating a float variable in Pandas, and I want the ranks to be unique (without duplicate ranks in case of links).

Here's what happens:

vals = pd.Series([0.0133, 0.0018, np.nan, 0.0006, 0.0006]) vals.rank(ascending=False, method='dense') 0 1.0 1 2.0 2 NaN 3 3.0 4 3.0 

I would like the result to be

 0 1.0 1 2.0 2 NaN 3 3.0 4 4.0 

Can I do this using the rank method, or do I need to do this manually using some sorting and looping logic?

+5
source share
2 answers

You can use first for the method (see Series.rank docs):

first: ranks assigned so that they appear in the array

 ser = pd.Series([1, 2, np.nan, 3, 3, 4]) ser.rank(method='first') Out: 0 1.0 1 2.0 2 NaN 3 3.0 4 4.0 5 5.0 dtype: float64 
+6
source

To clarify ayhan's answer to this (since I don't have enough reputation for editing or comments!)

df.rank(method=first) will work only if DF is sorted the way you want.

So, you need to sort your framework first with df.sort_values() , then you can rank it with df.rank(method=first) .

+1
source

Source: https://habr.com/ru/post/1260048/


All Articles