Pandas: group by index value, then calculate quantile?

You have to sort_index before rank:

import pandas as pd

df = pd.read_csv('http://pastebin.com/raw/6xbjvEL0')

df.month = pd.to_datetime(df.month, unit="s")
df = df.set_index('month')

df = df.sort_index()

df['percentile'] = df.groupby(df.index)['ratio_cost'].rank(pct=True)
print df['percentile'].head()

month
2010-08-01    0.2500
2010-08-01    0.6875
2010-08-01    0.6250
2010-08-01    0.9375
2010-08-01    0.7500
Name: percentile, dtype: float64

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)