How to round up Pandas column values?

In today’s tutorial we’ll learn how to round values in Pandas DataFrame columns. We’ll look into several cases:

  • Round float values to the nearest 2 decimals
  • Round float values to nearest 10 / 100
  • Rounding a Series
  • Persisting changes after rounding up

Example DataFrame

Let’s get starting by creating a sample DataFrame that you can use to follow along:

import pandas as pd

month = ['January', 'September', 'May', 'December']
language = ['Kotlin', 'VisualBasic', 'Java', 'C']
salary = [85.504, 84.22, 86.22, 86.55]

hr = dict(month=month, language=language, salary=salary)
df = pd.DataFrame(data=hr)

Let’s look into the DataFrame values:


Rounding specific columns to nearest two decimals

In our case we would like to take care of the salary column. We’ll use the round DataFrame method and pass a dictionary containing the column name and the number of decimal places to round to.

df.round(decimals = {'salary': 2})

Here is the result:


Note that if you have multiple columns to be rounded you should pass them to the dictionary accordingly.

#pseudo code
df.round(decimals = {'col1': <decimals_col1, 'coln': <decimals_coln> })

Rounding up column values to nearest 10 / 100

In the same fashion we can use a negative number in the decimals parameter to round up/down to nearest 1000 /100 /10 etc’:

# nearest 10
df.round(decimals = {'salary': -1})

#nearest 100
df.round(decimals = {'salary': -2})

Rounding a single column (Series)


Persisting changes in your DataFrame

The round method doesn’t have an inplace parameter; therefore if you would like to save your rounded values, you need to assign them into a new DataFrame:

df_rounded = df.round(decimals = {'salary': 2})

Suggested Learning

How to convert Pandas columns to integers and handle NAN values?