[英]pandas series/frame quantile function taking multiple probabilities?
有沒有一種方法可以調用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)
您可以寫一個:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.