Round Number of Places After Decimal in Pandas DataFrame



To round number of places after the decimal, use the display.precision attribute of the Pandas.

At first, import the required Pandas library −

import pandas as pd

Create a DataFrame with 2 columns −

dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
      "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000]

   }
)

Use the set_option() method to set precision. We have set precision value 2 −

pd.set_option('display.precision', 2)

Example

Following is the complete code −

import pandas as pd

# Create DataFrame
dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
      "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000]

   }
)

print"DataFrame ...\n",dataFrame

pd.set_option('display.precision', 2)
print"\nUpdated dataframe with rounding decimal values...\n", dataFrame

Output

This will produce the following output −

DataFrame ...
        Car    Reg_Price
0       BMW  7000.50570
1     Lexus  1500.00000
2     Tesla  5000.95780
3   Mustang  8000.00000
4  Mercedes  9000.75768
5    Jaguar  6000.00000

Updated dataframe with rounding decimal values...
        Car   Reg_Price
0       BMW    7000.51
1     Lexus    1500.00
2     Tesla    5000.96
3   Mustang    8000.00
4  Mercedes    9000.76
5    Jaguar    6000.00
Updated on: 2021-09-16T08:41:36+05:30

465 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements