Pyspark:如何从 table 中提取统计信息?
Pyspark: how to extract statistics from a table?
我有一个 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|
我想写一个 returns 一个 table 的查询,其中包含与每个 ID count
相关的每小时的一些统计信息
例如,我想要一个 table 如下所示:
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
您可以使用 window 函数和聚合。我认为这符合您的要求:
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|
+-------+------------------+
我有一个 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|
我想写一个 returns 一个 table 的查询,其中包含与每个 ID count
例如,我想要一个 table 如下所示:
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
您可以使用 window 函数和聚合。我认为这符合您的要求:
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|
+-------+------------------+