Skip to content Skip to sidebar Skip to footer

Round Double Values And Cast As Integers

I have a data frame in PySpark like below. import pyspark.sql.functions as func df = sqlContext.createDataFrame( [(0.0, 0.2, 3.45631), (0.4, 1.4, 2.82945),

Solution 1:

You should use the round function and then cast to integer type. However, do not use a second argument to the round function. By using 2 there it will round to 2 decimal places, the cast to integer will then round down to the nearest number.

Instead use:

df2 = df.withColumn("col4", func.round(df["col3"]).cast('integer'))

Post a Comment for "Round Double Values And Cast As Integers"