Round off the values in column of pandas python

Round function is used to round off the values in column of pandas dataframe. Let’s see how to

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

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.73,47.76,55.61,74.67,31.55,77.31,85.47]}
df1 = pd.DataFrame(df1,columns=['Subject','Score'])
print(df1)

so the resultant dataframe will be

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 the values in column of pandas python 2

 

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 the resultant dataframe will be

Round off the values in column of pandas python 3

 

Round off the values in column of pandas python                                                                                                         Round off the values in column of pandas python

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.

    View all posts