簡體   English   中英

Pyspark:如何從表中提取統計信息?

[英]Pyspark: how to extract statistics from a table?

我有一張如下所示的表:

+--------------------+-------------------+-----+
|                  ID|               time|count|
+--------------------+-------------------+-----+
|378101ee32a648ef0...|2020-01-01 11:00:00| 2900|
|ff5d5840742d42beb...|2020-01-01 23:00:00| 1615|
|ff5d5840742d42beb...|2020-01-01 22:00:00| 1589|
|a06f198b200364fb0...|2020-01-01 01:00:00| 1571|
|18991cb9b06c4dbde...|2020-01-01 01:00:00| 1514|
|aaf20cfe4ebc98ca8...|2020-01-01 19:00:00| 1462|
|35e96b1170613db44...|2020-01-01 17:00:00| 1324|
|0eb82275984a3eef0...|2020-01-01 16:00:00| 1305|
|0eb82275984a3eef0...|2020-01-01 17:00:00| 1305|

我想編寫一個查詢,該查詢返回一個表,其中包含與每個 ID count相關的每小時的一些統計信息

例如,我想要一個如下所示的表格:

       time              mean     median     min    max    5thPercentile  95thPercentile
2020-01-01 00:00:00       33        27.5      2    2000       3.4            1300        
2020-01-01 10:00:00       33        27.5      2    2000       2.6            1120

您可以使用窗口函數和聚合。 我認為這可以滿足您的要求:

select time,
       avg(count),
       (max(case when tile = 10 then count end) +
        min(case when tile = 11 then count end)
       ) / 11,
       max(case when tile = 1 then count end) as percentile_05,
       max(case when tile = 19 then count end) as percentile_95
from (select t.*,
             ntile(20) over (partition by count) as tile
      from t
     ) t
group by time;

使用pyspark.sql創建熊貓的類似 DataFrame 的對象。

然后您可以調用describe()來查看有關您的數據的統計信息。

來自文檔的示例:

>>> df.describe(['age']).show()
+-------+------------------+
|summary|               age|
+-------+------------------+
|  count|                 2|
|   mean|               3.5|
| stddev|2.1213203435596424|
|    min|                 2|
|    max|                 5|
+-------+------------------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM