Ric*_*ard 16 python dataframe pandas
我有一个在month
列上索引的DataFrame (df = df.set_index('month')
如果相关则设置使用):
org_code ratio_cost
month
2010-08-01 1847 8.685939
2010-08-01 1848 7.883951
2010-08-01 1849 6.798465
2010-08-01 1850 7.352603
2010-09-01 1847 8.778501
Run Code Online (Sandbox Code Playgroud)
我想添加一个名为的新列quantile
,它将根据该月的值为每行分配一个分位数值ratio_cost
.
所以上面的例子可能如下所示:
org_code ratio_cost quantile
month
2010-08-01 1847 8.685939 100
2010-08-01 1848 7.883951 66.6
2010-08-01 1849 6.798465 0
2010-08-01 1850 7.352603 33.3
2010-09-01 1847 8.778501 100
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我试过这个:
df['quantile'] = df.groupby('month')['ratio_cost'].rank(pct=True)
Run Code Online (Sandbox Code Playgroud)
但我明白了KeyError: 'month'
.
更新:我可以重现这个bug.
这是我的CSV文件:http://pastebin.com/raw/6xbjvEL0
这是重现错误的代码:
df = pd.read_csv('temp.csv')
df.month = pd.to_datetime(df.month, unit='s')
df = df.set_index('month')
df['percentile'] = df.groupby(df.index)['ratio_cost'].rank(pct=True)
print df['percentile']
Run Code Online (Sandbox Code Playgroud)
我在OSX上使用Pandas 0.17.1.
jez*_*ael 18
你必须在sort_index
之前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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
48822 次 |
最近记录: |