In this tutorial, you’ll learn how to round values in a Pandas DataFrame, including using the .round()
method. As you work with numerical data in Python, it’s essential to have a good grasp of rounding techniques to present and analyze your data effectively. In this tutorial, we’ll dive deep into various methods to round values in a Pandas DataFrame, ensuring your data is clean, structured, and understandable.
By the end of this tutorial, you’ll have learned the following:
- Rounding an entire Pandas DataFrame or column
- Rounding to a specific precision
- Rounding up or down using NumPy’s
ceil
andfloor
functions - Rounding and formatting as currency using
format
Table of Contents
The Quick Answer: Rounding Values in Pandas
If you’re in a hurry, check out the code block below. In order to round values in Pandas, you can use the .round()
method:
# The Quick Answer: Round Values in Pandas
import pandas as pd
import numpy as np
df.round() # Round a DataFrame
df.round(1) # Round to Specific Precision
df['col'].round() # Round a Column
df['col'].apply(np.ceil) # Round Up
df['col'].apply(np.floor) # Round Down
Loading a Sample Pandas DataFrame
To follow along with this tutorial, we’ll load a simple Pandas DataFrame to follow along with. The data is kept deliberately simple so that you can better understand what’s going on when we’re rounding data.
# Loading a Sample Pandas DataFrame
import pandas as pd
data = {
'Column1': [1.35, 3.78, 5.12],
'Column2': [2.4671, -4.8912, 6.5323]
}
df = pd.DataFrame(data)
print(df)
# Returns:
# Column1 Column2
# 0 1.35 2.4671
# 1 3.78 -4.8912
# 2 5.12 6.5323
You can see that in the dataset above, we have two columns. One contains values with two degrees of precision; the second contains three and some negative values. Let’s now dive into how rounding values in Pandas works.
Rounding All Values in a Pandas DataFrame
Pandas provides a helpful method for rounding values, both for a single column and for an entire DataFrame. In order to round a Pandas DataFrame, you can apply the .round()
method to it. By default, Pandas will round to the nearest whole number, meaning no decimal precision.
Let’s see how we can use the Pandas .round()
method to round our entire DataFrame:
# Round an entire DataFrame
import pandas as pd
data = {'Column1': [1.35, 3.78, 5.12], 'Column2': [2.4671, -4.8912, 6.5323]}
df = pd.DataFrame(data)
df = df.round()
print(df)
# Returns:
# Column1 Column2
# 0 1.0 2.0
# 1 4.0 -5.0
# 2 5.0 7.0
In the code block above, we applied the .round()
method to the DataFrame. Notice that all the values were rounded to the nearest whole number, however, the data type of the column was maintained.
In the following section, you’ll learn how to round values in a Pandas DataFrame to a specific precision.
Rounding All Values in a Pandas DataFrame to a Specific Precision
By default, the Pandas .round()
method will round values to 0 degrees of precision. In order to round values to a specific precision, you can pass an integer into the .round()
method.
Let’s see how we can round all values to one decimal precision in Pandas:
# Round an Entire DataFrame to a Specific Precision
import pandas as pd
data = {'Column1': [1.35, 3.78, 5.12], 'Column2': [2.4671, -4.8912, 6.5323]}
df = pd.DataFrame(data)
df = df.round(1)
print(df)
# Returns:
# Column1 Column2
# 0 1.4 2.5
# 1 3.8 -4.9
# 2 5.1 6.5
In the code block above, we passed in the value of 1
into the .round()
method. This instructs Pandas to round values to a single point of precision.
Round a Single Pandas DataFrame Column
In order to round a single Pandas DataFrame column, we can apply the .round()
method to a particular column. This works in the same way, allowing us to specify the degrees of precision to use. Let’s take a look at how we can round a single Pandas column:
# Round a Single DataFrame Column
import pandas as pd
data = {'Column1': [1.35, 3.78, 5.12], 'Column2': [2.4671, -4.8912, 6.5323]}
df = pd.DataFrame(data)
df['Column1'] = df['Column1'].round()
print(df)
# Returns:
# Column1 Column2
# 0 1.0 2.4671
# 1 4.0 -4.8912
# 2 5.0 6.5323
We can see that we re-assigned a column to its rounded self. By doing this, we were able to round only a single column.
Notice that the .round()
method doesn’t have any additional parameters, such as those allowing us to round a value up or down. Let’s explore how to do this in the following section.
Round a Single Pandas DataFrame Column Up
In order to round values in a Pandas DataFrame column up, we can combine the .apply()
method with NumPy’s or math’s ceil()
function. The .apply()
method allows us to apply a function to a column.
Python allows us to access the ceiling value (meaning the higher integer) using two easy functions: math.ceil()
and numpy.ceil()
. In this example, we’ll use the NumPy version of this:
# Round a Pandas DataFrame Column Up
import pandas as pd
import numpy as np
data = {'Column1': [1.35, 3.78, 5.12], 'Column2': [2.4671, -4.8912, 6.5323]}
df = pd.DataFrame(data)
df['Column2'] = df['Column2'].apply(np.ceil)
print(df)
# Returns:
# Column1 Column2
# 0 1.35 3.0
# 1 3.78 -4.0
# 2 5.12 7.0
Let’s break down what we’re doing in the code block above:
- We’re import NumPy, using the alias
np
- We then use the
.apply()
method to pass in the callable of theceil
function
In the following section, you’ll learn how to round a column down.
Round a Single Pandas DataFrame Column Down
In order to round values in a Pandas DataFrame column up, we can combine the .apply()
method with NumPy’s or math’s floor()
function.
Python allows us to access the floor value (meaning the lower integer) using two easy functions: math.floor()
and numpy.floor()
. In this example, we’ll use the NumPy version of this:
# Round a Pandas DataFrame Column Up
import pandas as pd
import numpy as np
data = {'Column1': [1.35, 3.78, 5.12], 'Column2': [2.4671, -4.8912, 6.5323]}
df = pd.DataFrame(data)
df['Column2'] = df['Column2'].apply(np.ceil)
print(df)
# Returns:
# Column1 Column2
# 0 1.35 3.0
# 1 3.78 -4.0
# 2 5.12 7.0
Let’s break down what we’re doing in the code block above:
- We’re import NumPy, using the alias
np
- We then use the
.apply()
method to pass in the callable of thefloor
function
In the following section, we’ll explore a unique application of the .round()
method – rounding values to a negative precision.
Rounding to a Negative Precision in Pandas
There might be situations where you want to round values to a specific negative precision, which essentially means rounding off to the nearest tens, hundreds, or even thousands. This can be particularly useful when dealing with large numbers or when you need to simplify your data for better readability.
In this section, we’ll learn how to round values in a Pandas DataFrame to negative precision using the round()
method. Let’s take a look at what this looks like:
# Round a Pandas DataFrame To Negative Precision
import pandas as pd
data = {'Column1': [1.35, 3.78, 5.12], 'Column2': [2.4671, -4.8912, 6.5323]}
df = pd.DataFrame(data)
df = df.round(-1)
print(df)
# Returns:
# Column1 Column2
# 0 0.0 0.0
# 1 0.0 -0.0
# 2 10.0 10.0
In the example above, we passed in -1
. This means that values are rounded to the nearest multiple of ten. This can be particularly useful when you want to make values more legible. Though, keep in mind you are losing precision by doing this, as with any rounding.
Round Values to Currency Formats in Pandas
In this final section, we’ll explore how to round values to currency formats. In order to accomplish this, we’ll make use of the styler functions that Pandas has built-in. This allows you to style your DataFrame in a given way, without losing the underlying data.
When working with financial data, it’s often necessary to round and format the values as currency. This not only makes the data more readable but also ensures that the values are presented correctly with the appropriate currency symbol and decimal places.
Keep in mind that the styling API will only work with Jupyter notebooks, or if you export the data to HTML.
# Round a Pandas DataFrame as Currency
import pandas as pd
import numpy as np
data = {'Column1': [1.35, 3.78, 5.12], 'Column2': [2.4671, -4.8912, 6.5323]}
df = pd.DataFrame(data)
# Format Column2 as currency
styled_df = df.style.format({'Column2': '${:,.2f}'})
styled_df
# Returns:
# Column1 Column2
# 0 1.350000 $2.47
# 1 3.780000 $-4.89
# 2 5.120000 $6.53
This allows you to format the values without overwriting the underlying data.
If you want the data to present as currency even when printing to the console, you will convert the data to strings. In order to do this, simply use the following df['Column2'].apply(lambda x: f"${x:,.2f}")
. This will apply the string formatting to the column, but you won’t be able to apply mathematical operations to it any longer.
Conclusion
Congratulations on completing this comprehensive tutorial on rounding values in a Pandas DataFrame! You’ve learned various techniques to round and format your data, including rounding to a specific precision, rounding single columns, rounding up or down using NumPy’s ceil
and floor
functions, rounding to negative precision, and rounding and formatting as currency.
By applying these techniques, you can present your data in a clean, structured, and easy-to-understand manner, making it more accessible for further analysis and decision-making.
If you’re interested in exploring more rounding techniques, be sure to check out our other resources on rounding in Python:
Remember, practice makes perfect! Keep experimenting with these rounding techniques on your own datasets to gain a deeper understanding and become more proficient in handling numerical data with Pandas.
To learn more about the Pandas .round()
method, check out the official documentation.