我正在尝试计算 DataFrame 中列的百分位数?我在 Spark 聚合函数中找不到任何 percentile_approx 函数。
例如在 Hive 中,我们有 percentile_approx,我们可以通过以下方式使用它
hiveContext.sql("select percentile_approx("Open_Rate",0.10) from myTable);
但出于性能原因,我想使用 Spark DataFrame 来做到这一点。
样本数据集
|User ID|Open_Rate|
-------------------
|A1 |10.3 |
|B1 |4.04 |
|C1 |21.7 |
|D1 |18.6 |
我想知道有多少用户属于 10% 或 20% 等等。我想做这样的事情
df.select($"id",Percentile($"Open_Rate",0.1)).show
最佳答案
从 Spark2.0 开始,事情变得更容易了,只需在 DataFrameStatFunctions 中使用此函数,例如:df.stat.approxQuantile("Open_Rate",Array(0.25,0.50,0.75),0.0)
DataFrameStatFunctions 中还有一些有用的 DataFrame 统计函数。
关于scala - 如何计算spark中DataFrame中列的百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37668343/