如何 include/map 计算结果数据帧的百分位数?

How to include/map calculated percentiles to the result dataframe?

我正在使用 spark-sql-2.4.1v,我正在尝试在给定数据的每一列上找到分位数,即百分位数 0、百分位数 25 等。

因为我在做多个百分位数,如何从结果中检索每个计算的百分位数?

我的数据框 df:

+----+---------+-------------+----------+-----------+
|  id|     date|      revenue|con_dist_1| con_dist_2|
+----+---------+-------------+----------+-----------+
|  10|1/15/2018|  0.010680705|         6|0.019875458|
|  10|1/15/2018|  0.006628853|         4|0.816039063|
|  10|1/15/2018|   0.01378215|         4|0.082049528|
|  10|1/15/2018|  0.010680705|         6|0.019875458|
|  10|1/15/2018|  0.006628853|         4|0.816039063|
+----+---------+-------------+----------+-----------+

我需要达到如下预期 output/result:

+----+---------+-------------+-------------+------------+-------------+
|  id|     date|      revenue| perctile_col| quantile_0 |quantile_10  |
+----+---------+-------------+-------------+------------+-------------+
|  10|1/15/2018|  0.010680705| con_dist_1  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|  0.010680705| con_dist_2  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|  0.006628853| con_dist_1  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|  0.006628853| con_dist_2  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|   0.01378215| con_dist_1  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|   0.01378215| con_dist_2  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|  0.010680705| con_dist_1  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|  0.010680705| con_dist_2  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|  0.006628853| con_dist_1  |<quant0_val>|<quant10_val>|
|  10|1/15/2018|  0.006628853| con_dist_2  |<quant0_val>|<quant10_val>|
+----+---------+-------------+-------------+------------+-------------+

我已经像这样计算了分位数,但需要将它们添加到输出数据框中:

val col_list = Array("con_dist_1","con_dist_2")
val quantiles = df.stat.approxQuantile(col_list, Array(0.0,0.1,0.5),0.0)

val percentile_0 = 0;
val percentile_10 = 1;

val Q0 = quantiles(col_list.indexOf("con_dist_1"))(percentile_0)
val Q10 =quantiles(col_list.indexOf("con_dist_1"))(percentile_10)

如何获得上面显示的预期输出?

一个简单的解决方案是创建多个数据框,每个 "con_dist" 列一个,然后使用 union 将它们合并在一起。这可以通过 col_list 使用 map 轻松完成,如下所示:

val col_list = Array("con_dist_1", "con_dist_2")
val quantiles = df.stat.approxQuantile(col_list, Array(0.0,0.1,0.5), 0.0)

val df2 = df.drop(col_list: _*) // we don't need these columns anymore

val result = col_list
  .zipWithIndex
  .map{case (col, colIndex) => 
    val Q0 = quantiles(colIndex)(percentile_0)
    val Q10 = quantiles(colIndex)(percentile_10)

    df2.withColumn("perctile_col", lit(col))
      .withColumn("quantile_0", lit(Q0))
      .withColumn("quantile_10", lit(Q10))
  }.reduce(_.union(_))

最终数据帧将是:

+---+---------+-----------+------------+-----------+-----------+
| id|     date|    revenue|perctile_col| quantile_0|quantile_10|
+---+---------+-----------+------------+-----------+-----------+
| 10|1/15/2018|0.010680705|  con_dist_1|        4.0|        4.0|
| 10|1/15/2018|0.006628853|  con_dist_1|        4.0|        4.0|
| 10|1/15/2018| 0.01378215|  con_dist_1|        4.0|        4.0|
| 10|1/15/2018|0.010680705|  con_dist_1|        4.0|        4.0|
| 10|1/15/2018|0.006628853|  con_dist_1|        4.0|        4.0|
| 10|1/15/2018|0.010680705|  con_dist_2|0.019875458|0.019875458|
| 10|1/15/2018|0.006628853|  con_dist_2|0.019875458|0.019875458|
| 10|1/15/2018| 0.01378215|  con_dist_2|0.019875458|0.019875458|
| 10|1/15/2018|0.010680705|  con_dist_2|0.019875458|0.019875458|
| 10|1/15/2018|0.006628853|  con_dist_2|0.019875458|0.019875458|
+---+---------+-----------+------------+-----------+-----------+