Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round decimal in Scala Spark

I have a (large ~ 1million) Scala Spark DataFrame with the following data:

id,score
1,0.956
2,0.977
3,0.855
4,0.866
...

How do I discretise/round the scores to the nearest 0.05 decimal place?

Expected result:

id,score
1,0.95
2,1.00
3,0.85
4,0.85
...

Would like to avoid using UDF to maximise performance.

like image 255
Ivan Avatar asked Nov 28 '22 13:11

Ivan


1 Answers

The answer can be simplifier:

dataframe.withColumn("rounded_score", round(col("score"), 2))

there is a method

def round(e: Column, scale: Int)

Round the value of e to scale decimal places with HALF_UP round mode

like image 50
irisha_murrr Avatar answered Dec 04 '22 01:12

irisha_murrr