In pandas, the round()
method is used to round the values in a DataFrame to a specified number of decimal places. This function allows you to round different columns to different numbers of decimal places, offering great flexibility. This is particularly useful for controlling the display of floating-point numbers or preparing data for analysis where numeric precision is important.
In this article, I will explain the Pandas DataFrame round()
method by using its syntax, parameters, and usage how we can return a new DataFrame with the values rounded to the specified number of decimal places. The original DataFrame remains unchanged.
Key Points –
- The
round()
method is used to round the values in a DataFrame to a specified number of decimal places. - By default,
round()
rounds all values to 0 decimal places (the nearest integer). - The
decimals
parameter can accept an integer to round all columns to the same number of decimal places or a dictionary to specify different decimal places for each column. - The method returns a new DataFrame with the rounded values, leaving the original DataFrame unchanged.
- It is useful for controlling the precision of floating-point numbers, especially when preparing data for analysis, presentation, or export.
Pandas DataFrame round() Introduction
Let’s know the syntax of the round() method.
# Syntax of Pandas DataFrame round() method
DataFrame.round(decimals=0, *args, **kwargs)
Parameters of the DataFrame round()
Following are the parameters of the DataFrame round() method.
decimals
– int or dict. This parameter specifies the number of decimal places to round each column to. If an integer is provided, all columns are rounded to that number of decimal places. If a dictionary is provided, it should map column names to the desired number of decimal places.args
,kwargs
– Additional arguments passed to the underlying round function of the data.
Return Value
It returns a DataFrame or Series with the values rounded to the specified number of decimal places.
Usage of Pandas DataFrame round() Method
The round()
method in Pandas rounds the values in a DataFrame to a specified number of decimal places. By default, it rounds to 0 decimal places, meaning it will round the values to the nearest integer.
Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are A
, B
.
# Create DataFrame
import pandas as pd
data = ({
'A': [1.1234, 2.5678, 5.3610],
'B': [3.9876, 4.61803, 6.2765]
})
df = pd.DataFrame(data)
print("Original DataFrame:\n",df)
Yields below output.
Round All Columns to Two Decimal Places
To round all columns in the DataFrame to two decimal places, you can use the round()
method with 2
as the argument.
# Round all columns to 2 decimal places
df2 = df.round(2)
print("DataFrame Rounded to 2 Decimal Places:\n", df2)
In the above example, both columns A
and B
are rounded to two decimal places. The round(2)
method ensures that all numeric values in the DataFrame are formatted to two decimal places. This example yields the below output.
Round All Columns to No Decimal Places (Nearest Integer)
Alternatively, to round all columns of a Pandas DataFrame to no decimal places (i.e., the nearest integer), you can use the round()
method with the decimals
parameter set to 0
.
# Round all columns to 0 decimal places (nearest integer)
df2 = df.round(0)
print("DataFrame rounded to nearest integer:\n", df2)
# Output:
# DataFrame rounded to nearest integer:
# A B
# 0 1.0 4.0
# 1 3.0 5.0
# 2 5.0 6.0
In the above example, all numeric values in the DataFrame are rounded to the nearest integer. Note that the resulting DataFrame retains floating-point representation with .0
but effectively rounds the values to the nearest whole number.
Round Different Columns to Different Decimal Places
To round different columns in the DataFrame to different numbers of decimal places, you can pass a dictionary to the round()
method where each key is a column name and each value is the number of decimal places you want for that column.
# Round column 'A' to 1 decimal place
# And column 'B' to 3 decimal places
df2 = df.round({'A': 1, 'B': 3})
print("DataFrame Rounded to Different Decimal Places:\n", df2)
# Output:
# DataFrame Rounded to Different Decimal Places:
# A B
# 0 1.1 3.988
# 1 2.6 4.618
# 2 5.4 6.276
In this example, column A
is rounded to 1
decimal place. Column B
is rounded to 3
decimal places.
Round Values in a DataFrame with Missing Data
Similarly, to round values in a DataFrame that contains missing data (NaN), you can use the round()
method as usual. The method will round the numerical values, and the missing data (NaN) will remain unchanged.
import pandas as pd
import numpy as np
# Create DataFrame with missing data
data = {
'A': [1.2345, np.nan, 3.4567],
'B': [4.5678, 5.6789, np.nan]
}
df = pd.DataFrame(data)
# Round all columns to 1 decimal place
df2 = df.round(1)
print("DataFrame rounded with missing data:\n", df2)
# Output:
# DataFrame rounded with missing data:
# A B
# 0 1.2 4.6
# 1 NaN 5.7
# 2 3.5 NaN
In the above example, the round()
method rounds numerical values to the specified number of decimal places. Missing data (NaN
) in the DataFrame is unaffected by the round()
method.
Round a DataFrame with Mixed Data Types (Only Numerical Columns)
Finally, To round a DataFrame that contains mixed data types (such as numerical and string columns), you can use the round()
method directly. The method will automatically round only the numerical columns, leaving the non-numerical data unchanged.
import pandas as pd
# Create a DataFrame with mixed data types
data = {
'A': [1.2345, 2.5678, 3.4567],
'B': [4.5678, 5.6789, 6.7890],
'C': ['Spark', 'Pandas', 'Hadoop']
}
df = pd.DataFrame(data)
# Round all numerical columns to 2 decimal places
df2 = df.round(2)
print("\nDataFrame Rounded (Only Numerical Columns):\n", df2)
# Output:
# DataFrame Rounded (Only Numerical Columns):
# A B C
# 0 1.23 4.57 Spark
# 1 2.57 5.68 Pandas
# 2 3.46 6.79 Hadoop
Frequently Asked Questions on Pandas DataFrame round() Method
The round()
method rounds the numeric values in a DataFrame to the specified number of decimal places. It can round all columns to the same number of decimal places or round different columns to different numbers of decimal places.
To round all columns to a specific number of decimal places, pass an integer to the round()
method. For example, df.round(2)
will round all numeric columns to 2 decimal places.
The round()
method does not modify the original DataFrame. It returns a new DataFrame with the rounded values, leaving the original DataFrame unchanged.
The round()
method leaves missing data (NaN values) unchanged. It only rounds the numeric values present in the DataFrame.
If no arguments are provided, the round()
method defaults to rounding all numeric columns to 0 decimal places, which effectively rounds them to the nearest integer.
Conclusion
In this article, I have explained the Pandas DataFrame round()
method by using its syntax, parameters, and usage how we can return a DataFrame with the specified columns rounded to the given number of decimal places with example.
Happy Learning!!
Related Articles
- Pandas DataFrame any() Method
- Pandas DataFrame diff() Method
- Pandas DataFrame mode() Method
- Pandas DataFrame max() Function
- Pandas DataFrame mad() Method
- Pandas DataFrame copy() Function
- Pandas DataFrame cov() Method
- Pandas DataFrame ffill() Method
- Pandas DataFrame corrwith() Method
- Pandas DataFrame sample() Function