evacoder
evacoder

Reputation: 11

Is there a way to do rolling rank in Pandas?

I am trying to rank some values in one column over a rolling period of N days instead of having the ranking done over the entire set. I have seen several methods here using rolling_apply but I have read that this is no longer in python. For example, in the following table;

A
01-01-2013 100
02-01-2013 85
03-01-2013 110
04-01-2013 60
05-01-2013 20
06-01-2013 40

For the column A above, how can I have the rank as below for N = 3;

A Ranked_A
01-01-2013 100 NaN
02-01-2013 85 Nan
03-01-2013 110 1
04-01-2013 60 3
05-01-2013 20 3
06-01-2013 40 2

Upvotes: 1

Views: 1893

Answers (1)

BENY
BENY

Reputation: 323226

Yes we have some work around, still with rolling but need apply

df.A.rolling(3).apply(lambda x: pd.Series(x).rank(ascending=False)[-1])
01-01-2013    NaN
02-01-2013    NaN
03-01-2013    1.0
04-01-2013    3.0
05-01-2013    3.0
06-01-2013    2.0
Name: A, dtype: float64
 

Upvotes: 4

Related Questions