Round up, down and Round off to a decimal place in PySpark?

Spread the love

To Round up a column in PySpark, we use the ceil() function. And to round down a column in PySpark, we use the floor() function. And to round off to a decimal place in PySpark, we use the round() function.

Read a Dataset –

Let’s read a dataset to illustrate it. We will use the clothing store sales data.

from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
df = spark.read.format('csv') \
    .options(header='true', inferSchema='true') \
    .load('../data/clothing_store_sales.csv')
df.show(5)

Round up a column using the ceil() function –

syntax –

ceil(column_name)

To Round up a column in PySpark, we use the ceil() function. We just have to pass the name of the column to the ceil() function. Let’s round up the Net Sales column.

from pyspark.sql.functions import ceil, col
df.select("*",ceil(col('Net Sales')).alias('Net Sales Round Up')).show(5)

Round down a column using the floor() function –

syntax –

floor(column_name)

To Round down a column in PySpark, we use the floor() function. We just have to pass the name of the column to the floor() function.

from pyspark.sql.functions import floor, col
df.select("*",floor(col('Net Sales')).alias('Net Sales Round down')).show(5)

Round off to a Decimal Place using the round() function –

syntax –

round(col, n)

col – column name

n – Round to n decimal places

To round off to a decimal place in PySpark, we use the round() function.

from pyspark.sql.functions import round, col
df.select("*",round(col('Net Sales'), 2).alias('Net Sales Round off')).show(5)

Rating: 1 out of 5.

Leave a Reply