4 Methods to Round Values in Pandas DataFrame

Depending on the scenario, you may use either of the 4 methods below in order to round values in pandas DataFrame:

(1) Round to specific decimal places – Single DataFrame column

df['DataFrame column'].round(decimals=number of decimal places needed)

(2) Round up – Single DataFrame column

df['DataFrame column'].apply(np.ceil)

(3) Round down – Single DataFrame column

df['DataFrame column'].apply(np.floor)

(4) Round to specific decimals places – Entire DataFrame

df.round(decimals=number of decimal places needed)

Let’s now see how to apply the 4 methods to round values in pandas DataFrame.

How to Round Values in Pandas DataFrame in Practice

Method 1: Round to specific decimal places – Single DataFrame column

Suppose that you have a dataset which contains the following values (with varying-length decimal places):

Value
5.52132
6.572935
7.21
8.755
9.9989

You can then create a DataFrame to capture those values in Python:

from pandas import DataFrame

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

print(df)

The DataFrame would look like this in Python:

dataframe python

Let’s say that your goal is to round the values into 3 decimals places.

Recall that the first method to round to specific decimals places (for a single DataFrame column) is:

df['DataFrame Column'].round(decimals=number of decimal places needed)

Therefore, for our example, in order to perform the rounding to 3 decimals places, you’ll need to add this syntax:

df['Value'].round(decimals=3)

So the full Python code would look like this:

from pandas import DataFrame

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundThree = df['Value'].round(decimals=3)
print(roundThree)

You’ll notice that the values are now rounded to 3 decimals places:

How to Round Values in Pandas DataFrame

Alternatively, you could also use numpy to round the values to 3 decimals places (for a single DataFrame column):

np.round(df['DataFrame column'], decimals=number of decimal places needed)

So this is how the Python code would look like for our example:

from pandas import DataFrame
import numpy as np

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundThree = np.round(df['Value'], decimals=3)
print(roundThree)

You’ll get the same results using numpy:

How to Round Values in Pandas DataFrame

Method 2: Round up – Single DataFrame column

What if you want to round up the values in your DataFrame?

To accomplish this goal, you can use the second method to round up values:

df['DataFrame Column'].apply(np.ceil)

In the context of our example, you’ll need to use this syntax:

df['Value'].apply(np.ceil)

Here is the complete Python code to round the values up using numpy:

from pandas import DataFrame
import numpy as np

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundUp = df['Value'].apply(np.ceil) 
print (roundUp)

You’ll notice that all the values got rounded up:

round up pandas dataframe

Method 3: Round down – Single DataFrame column

If you need to round the values down, you can then use the third method:

df['DataFrame Column'].apply(np.floor)

For our example:

df['Value'].apply(np.floor)

And this is the full Python code to round the values down using numpy:

from pandas import DataFrame
import numpy as np

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundDown = df['Value'].apply(np.floor)
print (roundDown)

Run the Python code, and you’ll get:

round down pandas dataframe

So far, you’ve seen how to round values for a single DataFrame column.

But what if you’d like to round values across an entire DataFrame that contains multiple columns?

To accomplish this goal, you can use the fourth method below.

Method 4: Round to specific decimals places – Entire DataFrame

Suppose that you have a new dataset with multiple columns:

Values_1Values_2Text
5.5213222.7352AAA
6.57293511.82ABC
7.2123.75839XYZ
8.7554.22AABB
9.998915.1173PPPP

This is how the DataFrame would look like in Python:

from pandas import DataFrame

Sample = {'Values_1': [5.52132,6.572935,7.21,8.755,9.9989],
          'Values_2': [22.7352,11.82,23.75839,4.22,15.1173],
          'Text': ['AAA','ABC','XYZ','AABB','PPPP']                               
          }

df = DataFrame(Sample, columns= ['Values_1','Values_2','Text'])
print (df)

Once you run the code in Python, you’ll get:

Round values in pandas dataframe

Let’s say that your goal is to round the values to 2 decimals places across all the columns that contain numeric values (i.e., the Values_1 and Values_2 columns).

You can then use the fourth method to round the values for the entire DataFrame (for all the columns that contain numeric values):

df.round(decimals=number of decimal places needed)

And this is the code that you can use for our example:

from pandas import DataFrame

Sample = {'Values_1': [5.52132,6.572935,7.21,8.755,9.9989],
          'Values_2': [22.7352,11.82,23.75839,4.22,15.1173],
          'Text': ['AAA','ABC','XYZ','AABB','PPPP']                               
          }

df = DataFrame(Sample, columns= ['Values_1','Values_2','Text'])

roundTwoDecimals = df.round(decimals=2)
print (roundTwoDecimals)

You’ll see that the values are now rounded to 2 decimal places across the 2 columns that contain the numeric data:

4 Methods to Round Values in Pandas DataFrame

Alternatively, you can get the same results by using numpy:

np.round(df, decimals=number of decimal places needed)

So the complete Python code would look like this:

from pandas import DataFrame
import numpy as np

Sample = {'Values_1': [5.52132,6.572935,7.21,8.755,9.9989],
          'Values_2': [22.7352,11.82,23.75839,4.22,15.1173],
          'Text': ['AAA','ABC','XYZ','AABB','PPPP']                               
          }

df = DataFrame(Sample, columns= ['Values_1','Values_2','Text'])

roundTwoDecimals = np.round(df, decimals=2)
print (roundTwoDecimals)

You’ll get the same results using numpy:

4 Methods to Round Values in Pandas DataFrame