I need to apply spark bucketizer on below dataframe df. This is mockup data. Original dataframe has around 10k records.
instance name value percentage
A37 Histogram.ratio 1 0.20
A37 Histogram.ratio 20 0.34
A37 Histogram.ratio 50 0.04
A37 Histogram.ratio 500 0.13
A37 Histogram.ratio 2000 0.05
A37 Histogram.ratio 9000 0.32
A49 Histogram.ratio 1 0.50
A49 Histogram.ratio 20 0.24
A49 Histogram.ratio 25 0.09
A49 Histogram.ratio 55 0.12
A49 Histogram.ratio 120 0.06
A49 Histogram.ratio 300 0.08
I need to apply bucketizer after partitioning the dataframe by column instance. Each value in instance has different split array which is defined below
val splits_map = Map("A37" -> Array(0,30,1000,5000,9000), "A49" -> Array(0,10,30,80,998))
i will perform bucketing on single column using below code. But need help in partitioning the dataframe by instance column and then applying bucketizer.transform
val bucketizer = new Bucketizer().setInputCol("value").setOutputCol("value_range").setSplits(splits)
val df2 = bucketizer.transform(df)
df2.groupBy("value_range").sum("percentage").show()
Is it possible to split dataFrame into multiple dataFrame with column value instance then bucketize the value column, then use groupBy().sum() to calculate the sum of percentage.
Expected output:
instance name bucket percentage
A37 Histogram.ratio 0 0.54
A37 Histogram.ratio 1 0.17
A37 Histogram.ratio 3 0.05
A37 Histogram.ratio 4 0.32
A49 Histogram.ratio 0 0.50
A49 Histogram.ratio 1 0.33
A49 Histogram.ratio 2 0.12
A49 Histogram.ratio 3 0.14