Round up, Round down and Round off in pyspark – (Ceil & floor pyspark)

Round up in pyspark or ceil in pyspark uses ceil() function which rounds up the column in pyspark. Round down in pyspark or floor in pyspark uses floor() function which rounds down the column in pyspark.  Round off the column in pyspark is accomplished by round() function. Let’s see an example of each.

  • Round up or Ceil in pyspark using ceil() function
  • Round down or floor in pyspark using floor() function
  • Round off the column in pyspark using round() function
  • Round off to decimal places in pyspark

We will be using dataframe df_states

Round up, Round down and Round off in pyspark – (Ceil & floor pyspark) 1

 

 

Round up or Ceil in pyspark using ceil() function

Syntax:

 ceil(‘colname1’)

colname1 – Column name

ceil() Function in pyspark takes up the column name as argument and rounds up the column

## Ceil or round up in pyspark

from pyspark.sql.functions import ceil, col
df_states.select("*", ceil(col('hindex_score'))).show()

ceil of “hindex_score” is shown below

Round up, Round down and Round off in pyspark – (Ceil & floor pyspark) 2

 

 

Round down or Floor in pyspark using floor() function

Syntax:

 floor(‘colname1’)

colname1 – Column name

floor() Function in pyspark takes up the column name as argument and rounds down the column

## floor or round down in pyspark

from pyspark.sql.functions import floor, col
df_states.select("*", floor(col('hindex_score'))).show()

floor of “hindex_score” is shown below

Round up, Round down and Round off in pyspark – (Ceil & floor pyspark) 3

 

 

 

Round off in pyspark using round() function

Syntax:

 round(‘colname1’,n)

colname1 – Column name
n – round to n decimal places

round() Function in pyspark takes up the column name as argument and rounds the column to nearest integers

######### round off

from pyspark.sql.functions import round, col
df_states.select("*", round(col('hindex_score'))).show()

rounding off of “hindex_score” column is shown below

Round up, Round down and Round off in pyspark – (Ceil & floor pyspark) 4

 

 

Round off to decimal places in pyspark using round() function

round() Function in pyspark takes up the column name and 2 as argument and rounds off the column to nearest two decimal place

########## round off to decimal places

from pyspark.sql.functions import round, col
df_states.select("*", round(col('hindex_score'),2)).show()

Rounding off of “hindex_score” column to 2 decimal places is shown below

Round up, Round down and Round off in pyspark – (Ceil & floor pyspark) 5

 

Round up, Round down and Round off in pyspark – (Ceil & floor pyspark)                                                                                             Round up, Round down and Round off in pyspark – (Ceil & floor pyspark)