• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pandas round – Round Numbers in Series or DataFrame

pandas round – Round Numbers in Series or DataFrame

December 20, 2021 Leave a Comment

To round numbers in a column or DataFrame using pandas, the easiest way is to use the pandas round() function.

df["Column"] = df["Column"].round()

If you want to round to a specific number of decimal places, you can pass the number of decimal places to the round() function.

df["Column"] = df["Column"].round(1)

Rounding numbers in a column in pandas is easy. We can round numbers in a column to the nearest integer with the pandas round() function.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [160.20, 123.81, 209.45, 150.35, 102.43, 187.52]})

print(df)
# Output: 
    Name  Weight
0    Jim  160.20
1  Sally  123.81
2    Bob  209.45
3    Sue  150.35
4   Jill  102.43
5  Larry  187.52

To round all of the numbers to the nearest integer in the column “weight”, we can use the pandas round() function in the following way:

df["Round of Weight"] = df["Weight"].round()

print(df)
# Output: 
    Name  Weight  Round of Weight
0    Jim  160.20            160.0
1  Sally  123.81            124.0
2    Bob  209.45            209.0
3    Sue  150.35            150.0
4   Jill  102.43            102.0
5  Larry  187.52            188.0

Rounding Numbers to Multiple Decimal Places Using pandas

We can also use the pandas round() function to round by number of decimal places. If we want to round to one decimal place, we can pass 1 to the round() function.

If we have the same DataFrame from above, you can see below how the pandas round() function will work if we want to round to the nearest decimal place.

df["Round of Weight"] = df["Weight"].round(1)

print(df)
# Output: 
    Name  Weight  Round of Weight
0    Jim  160.20            160.2
1  Sally  123.81            123.8
2    Bob  209.45            209.5
3    Sue  150.35            150.4
4   Jill  102.43            102.4
5  Larry  187.52            187.5

Using the numpy round Function to Round Numbers in DataFrame

We can also use the numpy round() function to round numbers to the nearest integer in our pandas Series and DataFrames.

If we have the same DataFrame from above, we can apply the np.round function in the following way to round all of the numbers in the “Weight” column.

df["Round of Weight"] = df["Weight"].apply(np.round)

print(df)

# Output:
    Name  Weight  Round of Weight
0    Jim  160.20            160.0
1  Sally  123.81            124.0
2    Bob  209.45            209.0
3    Sue  150.35            150.0
4   Jill  102.43            102.0
5  Larry  187.52            188.0

If you are looking to round to the nearest number in regular Python, you can use the Python round() function.

If you want to round down all the numbers in a column to the nearest integer, instead of rounding to the nearest integer , you can use the numpy floor() function.

To round up all the numbers in a column to the nearest integer, instead of rounding to the nearest integer, you can use the numpy ceil() function.

Hopefully this article has been helpful for you to use the pandas round() function to round numbers in a column using pandas in python.

Other Articles You'll Also Like:

  • 1.  Get Public IP Address Using Python
  • 2.  How to Check if Character is Uppercase in Python
  • 3.  Creating a List of Zeros in Python
  • 4.  Create Empty String Variable in Python
  • 5.  Using Python to Calculate Average of List of Numbers
  • 6.  Get pandas Series Last Element in Python
  • 7.  Intersection of Two Lists in Python
  • 8.  Get Current Year in Python
  • 9.  Python Replace Space with Underscore Using String replace() Function
  • 10.  pandas T Function – Transposing DataFrames with pandas

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy