[英]How should I calculate sum of field grouped by another one using Spring Data MongoDB
[英]How to calculate percentiles grouped by column using partitionedBy?
我正在使用 spark-sql-2.4.1v,並且我正在嘗試在給定數據的每一列上查找分位數,即百分位數 0、百分位數 25 等。 當我在做多個百分位數時,如何從結果中檢索每個計算的百分位數?
我的數據框df
:
+----+---------+-------------+----------+-----------+-------+
| id| date| revenue|con_dist_1| con_dist_2| zone |
+----+---------+-------------+----------+-----------+-------+
| 10|1/15/2018| 0.010680705| 10|0.019875458| east |
| 10|1/15/2018| 0.006628853| 4|0.816039063| west |
| 10|1/15/2018| 0.01378215| 20|0.082049528| east |
| 10|1/15/2018| 0.010680705| 6|0.019875458| west |
| 10|1/15/2018| 0.006628853| 30|0.816039063| east |
+----+---------+-------------+----------+-----------+-------+
最終的數據幀應該如下所示,即每個區域:
+---+---------+-----------+-------+-------------+-----------+-----------+
| id| date| revenue| zone | perctile_col| quantile_0|quantile_10|
+---+---------+-----------+-------+-------------+-----------+-----------+
| 10|1/15/2018|0.010680705| east | con_dist_1 | 10.0| 30.0|
| 10|1/15/2018|0.010680705| east | con_dist_2 |0.019875458|0.816039063|
| 10|1/15/2018|0.010680705| west | con_dist_1 | 4.0| 6.0|
| 10|1/15/2018|0.010680705| west | con_dist_2 |0.019875458|0.816039063|
+---+---------+-----------+-------+-------------+-----------+-----------+
有沒有辦法使用partitionBy
和approxQuantile
函數? 這是否會使用repartition("zone")
,即不收集每個區域的數據集?
approxQuantile
在這里不太合適,因為它不允許分組。 然而,這個問題可以通過使用percentile_approx
和 Spark 窗口函數來解決( groupBy
在這里也可以使用,使用哪個取決於所需的數據幀格式)。 首先我們做一些設置:
val df = Seq(
(10, "1/15/2018", 0.010680705, 10,0.019875458, "east"),
(10, "1/15/2018", 0.006628853, 4,0.816039063, "west"),
(10, "1/15/2018", 0.01378215, 20,0.082049528, "east"),
(10, "1/15/2018", 0.010680705, 6,0.019875458, "west"),
(10, "1/15/2018", 0.006628853, 30,0.816039063, "east"))
.toDF("id", "date", "revenue", "con_dist_1", "con_dist_2", "zone")
val percentiles = Seq(0.1, 1.0) // Which percentiles to calculate
val cols = Seq("con_dist_1", "con_dist_2") // The columns to use
要計算每個區域組的百分位數,可以按如下方式完成:
val window = Window.partitionBy("zone")
val percentile_func = (col: String) => expr(s"percentile_approx(${col}, array(${percentiles.mkString(",")}))")
val df2 = cols.foldLeft(df){case (df, c) => df.withColumn(c, percentile_func(c).over(window))}
結果將是這樣的:
+---+---------+-----------+----------+--------------------------+----+
|id |date |revenue |con_dist_1|con_dist_2 |zone|
+---+---------+-----------+----------+--------------------------+----+
|10 |1/15/2018|0.006628853|[4, 6] |[0.019875458, 0.816039063]|west|
|10 |1/15/2018|0.010680705|[4, 6] |[0.019875458, 0.816039063]|west|
|10 |1/15/2018|0.010680705|[10, 30] |[0.019875458, 0.816039063]|east|
|10 |1/15/2018|0.01378215 |[10, 30] |[0.019875458, 0.816039063]|east|
|10 |1/15/2018|0.006628853|[10, 30] |[0.019875458, 0.816039063]|east|
+---+---------+-----------+----------+--------------------------+----+
接下來,我們要將數據幀轉換為正確的格式。 這是對這里答案的輕微改編:如何將計算的百分位數包含/映射到結果數據幀? .
cols.map{ case c =>
percentiles
.zipWithIndex
.foldLeft(df2.withColumn("perctile_col", lit(c))){ case (df2, (perc, index)) =>
df2.withColumn(s"qunantile_${perc}", col(c).getItem(index))
}
}
.reduce(_.union(_))
.drop(cols: _*) // these are not needed anymore
最終數據框:
+---+---------+-----------+----+------------+-------------+-------------+
| id| date| revenue|zone|perctile_col|qunantile_0.1|qunantile_1.0|
+---+---------+-----------+----+------------+-------------+-------------+
| 10|1/15/2018|0.006628853|west| con_dist_1| 4.0| 6.0|
| 10|1/15/2018|0.010680705|west| con_dist_1| 4.0| 6.0|
| 10|1/15/2018|0.010680705|east| con_dist_1| 10.0| 30.0|
| 10|1/15/2018| 0.01378215|east| con_dist_1| 10.0| 30.0|
| 10|1/15/2018|0.006628853|east| con_dist_1| 10.0| 30.0|
| 10|1/15/2018|0.006628853|west| con_dist_2| 0.019875458| 0.816039063|
| 10|1/15/2018|0.010680705|west| con_dist_2| 0.019875458| 0.816039063|
| 10|1/15/2018|0.010680705|east| con_dist_2| 0.019875458| 0.816039063|
| 10|1/15/2018| 0.01378215|east| con_dist_2| 0.019875458| 0.816039063|
| 10|1/15/2018|0.006628853|east| con_dist_2| 0.019875458| 0.816039063|
+---+---------+-----------+----+------------+-------------+-------------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.