
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Round number of places after the decimal for column values in a 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
- Related Articles
- Python - Calculate the mean of column values of a Pandas DataFrame
- Python - Calculate the median of column values of a Pandas DataFrame
- Python - Calculate the count of column values of a Pandas DataFrame
- Python - Calculate the maximum of column values of a Pandas DataFrame
- Python - Calculate the minimum of column values of a Pandas DataFrame
- Python – Group and calculate the sum of column values of a Pandas DataFrame
- How to round a number to n decimal places in Java
- Java Program to Round a Number to n Decimal Places
- Swift Program to Round a Number to n Decimal Places
- Haskell Program to Round a Number to n Decimal Places
- Kotlin Program to Round a Number to n Decimal Places
- C++ Program to Round a Number to n Decimal Places
- Merge Python Pandas dataframe with a common column and set NaN for unmatched values
- How to count the NaN values in a column in a Python Pandas DataFrame?
- Python - Display True for infinite values in a Pandas DataFrame

Advertisements