Ceil(),floor() & Round() the dataframe in pandas python – Round up and Truncate

Ceil(),floor() & Round() of the dataframe in pandas python gets the rounded up, truncated values and rounded off value of the column. Ceil() function in pandas python is used to round up the column of the dataframe. floor() function in pandas python is used to round down or truncate the column of the dataframe. round() function in pandas is used to round off the value to the specified decimal places.  Let’s see how to

• Ceil() and floor() function in python
• Get the rounded up value of column in pandas dataframe using ceil() function
• Get the rounded down value or truncated value of column in pandas dataframe using floor() function
• Round off the values of column to one decimal place in pandas dataframe
• Round off values of column to two decimal place in pandas dataframe

floor() function in python:

floor() method in Python returns floor of x i.e., the rounded down integer not greater than x.

Syntax:
import math
math.floor(x)

Below is the Python way of implementation of floor() method:

#### import math
import math

# prints the floor using floor() method
print "math.floor(-43.13) : ", math.floor(-43.13)
print "math.floor(200.26) : ", math.floor(200.26)
print "math.floor(400.72) : ", math.floor(400.72)

so the output will be

math.floor(-43.13) :  -44.0
math.floor(200.26) :  200.0
math.floor(400.72) :  400.0

ceil() function in python:

ceil() method in Python returns ceil of x i.e., the rounded up integer which is greater than x.

Syntax:
import math
math.ceil(x)

Below is the Python way of implementation of ceil() method:

#### import math
import math

# prints the ceil using ceil() method
print "math.ceil(-43.13) : ", math.ceil(-43.13)
print "math.ceil(200.26) : ", math.ceil(200.26)
print "math.ceil(400.72) : ", math.ceil(400.72)

so the output will be

math.ceil(-43.13) :  -43.0
math.ceil(200.26) :  201.0
math.ceil(400.72) :  401.0

Ceil(),floor() & Round() of the dataframe in pandas

In order to demonstrate the Ceil(),floor() & Round() of the dataframe in pandas python First lets create the dataframe.

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
'Subject':['semester1','semester2','semester3','semester4','semester1',
'semester2','semester3'],
'Score':[62.7,47.7,55.6,74.6,31.5,77.3,85.4]}

df1 = pd.DataFrame(df1,columns=['Subject','Score'])
print(df1)

so the resultant dataframe will be

Get the ceil of column in pandas dataframe using ceil() function:

In the below example ceiling() function is applied to the column of a dataframe in which it takes up the column value as argument. which rounds up the column value without any decimal place.

df1['score_ceil'] = df1['Score'].apply(np.ceil)
print(df1)

so in the resultant dataframe “Score” column will be rounded up without decimal places and stored in another column named “score_ceil”

Get the floor of column in pandas dataframe using floor() function:

floor() gets the rounded down (truncated) values of column in dataframe. In the below example floor() function is applied to the column of a dataframe in which it takes up the column value as argument. which rounds down the column value without any decimal place.

df1['score_floor'] = df1['Score'].apply(np.floor)
print(df1)

so in the resultant dataframe “Score” column will be rounded down without decimal places and stored in another column named “score_floor”

Round off the column in pandas using round() function

round gets the rounded values of column in dataframe

df1['score_round_off']= round(df1['Score'])
print(df1)

so the resultant dataframe will be

Round off column to specified decimal place :

We can round off the column to n decimal place. Here let’s round of column to one decimal places. round function along with the argument  1 rounds off the column value to one decimal place as shown below

df1['score_rounded_off_single_decimal']= round(df1['Score'],1)
print(df1)

so in the resultant dataframe “Score” column will be rounded off to one decimal place

Author

• 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.