Chris Chris - 17 days ago 9
Python Question

Force incrementation in pandas rank method

I am ranking a float variable in Pandas and I want to force ranks to be unique (no duplicate ranks in the event of ties.)

This is 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 instead be

0 1.0
1 2.0
2 NaN
3 3.0
4 4.0


Can I do this with the
rank
method or do I have to do this manually with some sorting and looping logic?

Answer

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

first: ranks assigned in order 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