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

Round up or ceil in pyspark uses ceil() function which rounds up the column in pyspark. Round down or floor in pyspark uses floor() function which rounds down the column in pyspark.  Round off the column 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 using round() function.

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 takes up the column name as argument and rounds up the column and the resultant values are stored in the separate column as shown below

## Ceil or round up in pyspark

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

So the resultant dataframe with 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 and the resultant values are stored in the separate column as shown below

## floor or round down in pyspark

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

So the resultant dataframe with 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 takes up the column name as argument and rounds the column to nearest integers and the resultant values are stored in the separate column as shown below

######### round off

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

So the resultant dataframe with 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 using round() function

round() Function takes up the column name and 2 as argument and rounds off the column to nearest two decimal place and the resultant values are stored in the separate column as shown below

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

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

So the resultant dataframe with 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

 


Other Related Topics:

 

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

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.