rank data over a rolling window in pandas DataFrame

狂风中的少年 提交于 2019-11-29 14:17:20

If you want to use the Pandas built-in rank method (with some additional semantics, such as the ascending option), you can create a simple function wrapper for it

def rank(array):
    s = pd.Series(array)
    return s.rank(ascending=False)[len(s)-1]

that can then be used as a custom rolling-window function.

pd.rolling_apply(df['A'], 3, rank)

which outputs

Date
01-01-2013   NaN
02-01-2013   NaN
03-01-2013     1
04-01-2013     3
05-01-2013     3
06-01-2013     2

(I'm assuming the df data structure from Rutger's answer)

You can write a custom function for a rolling_window in Pandas. Using numpy's argsort() in that function can give you the rank within the window:

import pandas as pd
import StringIO

testdata = StringIO.StringIO("""
Date,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""")

df = pd.read_csv(testdata, header=True, index_col=['Date'])

rollrank = lambda data: data.size - data.argsort().argsort()[-1]

df['rank'] = pd.rolling_apply(df, 3, rollrank)

print df

results in:

              A  rank
Date                 
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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!