有没有办法调用frame.quantile(或series.quantile)并提供概率列表?看起来这只是代码中的遗漏,因为它依赖于scipy.stats.scoreatpercentile,它同时包含概率列表和轴参数。
In [10]: df = pandas.DataFrame(randn(100,10))
In [11]: df.shape
Out[11]: (100, 10)
In [12]: df.quantile().shape
Out[12]: (10,)
In [13]: df.quantile(q=array([0.1, 0.2, 0.3]))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-0e8c5e9629fc> in <module>()
----> 1 df.quantile(q=array([0.1, 0.2, 0.3])
....
比较:
scipy.stats.scoreatpercentile(randn(100,10), per=[0.1, 0.2], axis=0)
答案 0 :(得分:2)
你可以写一个:
def quantile_list(df, quantiles):
return type(df)({q: df.quantile(q) for q in quantiles})
# this will work for both Series and DataFrames
quantile_list(df, array([0.1, 0.2, 0.3]))
使用示例:
In [11]: df = pd.DataFrame(randn(100,10)
In [12]: quantile_list(df, [0.1, 0.2, 0.3])
Out[12]:
0.1 0.2 0.3
0 -1.170263 -0.873469 -0.646108
1 -1.022404 -0.710545 -0.475673
2 -1.052089 -0.624228 -0.284597
3 -1.258572 -0.768058 -0.511088
4 -1.326680 -0.711250 -0.496603
5 -1.251355 -0.927114 -0.585490
6 -1.288566 -0.855460 -0.554957
7 -1.434426 -1.021975 -0.675071
8 -1.151979 -0.705312 -0.465302
9 -1.228328 -0.836559 -0.450415
In [13]: quantile_list(df[1], [0.1, 0.2, 0.3])
Out[13]:
0.1 -1.022404
0.2 -0.710545
0.3 -0.475673
dtype: float64