search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | round method

Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Basic and Descriptive Statistics
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas DataFrame.round(~) method returns a DataFrame with all its numerical values rounded according to the specified parameters.

Parameters

1. decimals | int or dict or Series | optional

The number of decimals to round to. The columns that are rounded depends on the data type of decimals:

Type

Description

int

All values in the DataFrame will be rounded.

dict

Only the specified columns will be rounded.

Series

Only the specified columns will be rounded.

Note that decimals=1 would mean that values like 1.52 will be rounded to 1.5. In contrast, decimals=-1 would round values like 13 to 10 (nearest 10th).

As for data types dict and Series, the key and indexes will be the names of the columns you want to round, with the corresponding values being the number of decimals to round to. See examples below for clarification.

By default, decimals=0, which means that the values are rounded to the nearest integer.

WARNING

Numbers ending in 5 will be rounded down. For instance, numbers such as 2.5 and 3.45 will be rounded down to 2 and 3.4, respectively, instead of rounding up.

Return Value

A DataFrame with its values rounded according to the supplied parameter.

Examples

Rounding all values to nearest integer

Consider the following DataFrame:

df = pd.DataFrame({"A":[1.05,2.42],"B":[3.45,4.9]})
df
   A     B
0  1.05  3.45
1  2.42  4.90

To round to the nearest integer, just call round() directly:

df.round()
   A    B
0  1.0  3.0
1  2.0  5.0

Rounding all values to the nearest 10th

Consider the following DataFrame:

df = pd.DataFrame({"A":[4,6],"B":[12,18]})
df
   A  B
0  4  12
1  6  18

To round to the nearest 10th, supply -1 as the argument:

df.round(-1)
   A   B
0  0   10
1  10  20

Rounding all values to the 1st decimal place

Consider the following DataFrame:

df = pd.DataFrame({"A":[1.07,2.42],"B":[3.45,4.9]})
df
   A     B
0  1.07  3.45
1  2.42  4.90

To round to the 1st decimal place, supply 1 as the argument:

df.round(1)
   A    B
0  1.1  3.4
1  2.4  4.9

Notice how 3.45 was rounded down to 3.4 instead of rounding up to 3.5.

Rounding certain columns only

Instead of providing an int, we can provide either a dict or a Series to round certain columns.

Passing in Dict

When providing a dict, the key must be the column name, while the value must be the decimal place to round to.

As an example, consider the following DataFrame:

df = pd.DataFrame({"A":[4.2,6.6],"B":[12.4,18.9]})
df
   A    B
0  4.2  12.4
1  6.6  18.9

To round only column A to the nearest integer, and leave column B intact:

df.round({"A":0})
   A    B
0  4.0  12.4
1  7.0  18.9

Notice how column B was left as is.

Passing in Series

Similarly, when providing a Series, the indexes must be the column name, while the value must be the decimal place to round to.

Consider the following DataFrame:

df = pd.DataFrame({"A": [4.2,6.6], "B": [12.4,18.9]})
df
   A    B
0  4.2  12.4
1  6.6  18.9

To round only column A to the nearest integer, and leave column B intact:

decimals = pd.Series([0], index=["A"])
df.round(decimals)
   A    B
0  4.0  12.4
1  7.0  18.9

Notice how column B was left as is.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...