
round()
The round()
function rounds off numeric values in a column to a specified number of decimal places. round()
takes two arguments: the column to round and the number of decimal places to round to.
Create Spark Session and sample DataFrame
from pyspark.sql import SparkSessionfrom pyspark.sql.functions import round
# Initialize Spark Sessionspark = SparkSession.builder.appName("roundExample").getOrCreate()
# Sample DataFramedata = [(2.654,), (3.785,), (1.239,), (0.562,)]columns = ["Value"]df = spark.createDataFrame(data, columns)df.show()
Output:
+-----+
|Value|
+-----+
|2.654|
|3.785|
|1.239|
|0.562|
+-----+
Example: Use round()
to round float values to a specified number of decimals
round("Value", 2)
: it takes the Value column and rounds the float numbers to 2 decimal places.alias("Rounded Value")
: it renames the resulting column as Rounded Value.
rounded_df = df.select(round("Value", 2).alias("Rounded Value"))rounded_df.show()
Output:
+-------------+
|Rounded Value|
+-------------+
| 2.65|
| 3.79|
| 1.24|
| 0.56|
+-------------+
# Stop the Spark Sessionspark.stop()