Pandas Tutorial Pandas References

Pandas DataFrame - round() function



The Pandas DataFrame round() function rounds a DataFrame to a specified number of decimal places. The syntax for using this function is mentioned below:

Syntax

DataFrame.round(decimals=0)

Parameters

decimals Optional. Specify int, dict, Series to indicate number of decimal places to round each column to. If an int is provided, round each column to the same number of places. Otherwise dict and Series round to variable numbers of places. When dict is provided, keys should specify the column names which need to be rounded. When Series is provided, index should specify the column names which need to be rounded. Any columns not included in decimals will be left as is. Elements of decimals which are not columns of the input will be ignored.

Return Value

Returns a DataFrame with the affected columns rounded to the specified number of decimal places.

Example: Rounding whole DataFrame

In the example below, a DataFrame df is created. The round() function is used to round a DataFrame to a specified number of decimal places.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5.344, 3.925, 2.150, 4.229],
  "Salary": [60.227, 62.550, 65.725, 59.328]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#rounding the whole dataframe to 0 decimal places
print("\ndf.round() returns:")
print(df.round())

#rounding the whole dataframe to 1 decimal places
print("\ndf.round(2) returns:")
print(df.round(2))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John   5.344  60.227
Marry  3.925  62.550
Sam    2.150  65.725
Jo     4.229  59.328

df.round() returns:
       Bonus  Salary
John     5.0    60.0
Marry    4.0    63.0
Sam      2.0    66.0
Jo       4.0    59.0

df.round(2) returns:
       Bonus  Salary
John    5.34   60.23
Marry   3.92   62.55
Sam     2.15   65.72
Jo      4.23   59.33

Example: Rounding selected columns

Instead of whole DataFrame, the round() function can be applied on selected columns using a dictionary or a Series specifying which column need to be rounded. Consider the example below:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5.344, 3.925, 2.150, 4.229],
  "Salary": [60.227, 62.550, 65.725, 59.328],
  "Other perks":[2.227, 1.750, 2.527, 2.958]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df, "\n")

#using dict to specify which column to round
colToround1 = dict({'Bonus': 2, 'Other perks': 1})
print("\ndf.round(colToround1) returns:")
print(df.round(colToround1))

#using series to specify which column to round
colToround2 = pd.Series([1, 2, 1], 
    index=['Bonus', 'Salary', 'Other perks'])
print("\ndf.round(colToround2) returns:")
print(df.round(colToround2))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary  Other perks
John   5.344  60.227        2.227
Marry  3.925  62.550        1.750
Sam    2.150  65.725        2.527
Jo     4.229  59.328        2.958 


df.round(colToround1) returns:
       Bonus  Salary  Other perks
John    5.34  60.227          2.2
Marry   3.92  62.550          1.8
Sam     2.15  65.725          2.5
Jo      4.23  59.328          3.0

df.round(colToround2) returns:
       Bonus  Salary  Other perks
John     5.3   60.23          2.2
Marry    3.9   62.55          1.8
Sam      2.2   65.72          2.5
Jo       4.2   59.33          3.0

❮ Pandas DataFrame - Functions