4 Ways to Round Values in Pandas DataFrame

Here are 4 ways to round values in Pandas DataFrame:

(1) Round to specific decimal places under a single DataFrame column

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

(2) Round up values under a single DataFrame column

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

(3) Round down values under a single DataFrame column

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

(4) Round to specific decimals places under an entire DataFrame

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

Let’s now see how to apply the above approaches using practical examples.

4 Ways to Round Values in Pandas DataFrame

(1) Round to specific decimal places under a single DataFrame column

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

values
5.52132
6.572935
7.21
8.755
9.9989

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

import pandas as pd

data = {'values': [5.52132, 6.572935, 7.21, 8.755, 9.9989]}

df = pd.DataFrame(data)

print(df)

The DataFrame would look like this in Python:

     values
0  5.521320
1  6.572935
2  7.210000
3  8.755000
4  9.998900

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

Recall that you can round to specific decimals places (under a single DataFrame column) using:

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

Therefore, in order to round to 3 decimals places, you’ll need to use this syntax:

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

So the complete Python code would look like this:

import pandas as pd

data = {'values': [5.52132, 6.572935, 7.21, 8.755, 9.9989]}

df = pd.DataFrame(data)

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

print(df)

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

   values
0   5.521
1   6.573
2   7.210
3   8.755
4   9.999

Alternatively, you can use NumPy to round the values to 3 decimals places:

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

Here is the Python code:

import pandas as pd
import numpy as np

data = {'values': [5.52132, 6.572935, 7.21, 8.755, 9.9989]}

df = pd.DataFrame(data)

df['values'] = np.round(df['values'], decimals=3)

print(df)

You’ll get the same results using NumPy:

   values
0   5.521
1   6.573
2   7.210
3   8.755
4   9.999

(2) Round up values under a single DataFrame column

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

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

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

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

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

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

import pandas as pd
import numpy as np

data = {'values': [5.52132, 6.572935, 7.21, 8.755, 9.9989]}

df = pd.DataFrame(data)

df['values'] = df['values'].apply(np.ceil) 

print(df)

You’ll notice that all the values are now rounded up:

   values
0     6.0
1     7.0
2     8.0
3     9.0
4    10.0

(3) Round down values under a single DataFrame column

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

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

For our example:

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

And here is the full Python code to round the values down:

import pandas as pd
import numpy as np

data = {'values': [5.52132, 6.572935, 7.21, 8.755, 9.9989]}

df = pd.DataFrame(data)

df['values'] = df['values'].apply(np.floor)

print(df)

Run the code, and you’ll get:

   values
0     5.0
1     6.0
2     7.0
3     8.0
4     9.0

So far, you’ve seen how to round values under 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 approach below.

(4) Round to specific decimals places under an entire DataFrame

Suppose that you have a new dataset with multiple columns:

values_1values_2values_3
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:

import pandas as pd

data = {'values_1': [5.52132, 6.572935, 7.21, 8.755, 9.9989],
        'values_2': [22.7352, 11.82, 23.75839, 4.22, 15.1173],
        'values_3': ['AAA', 'ABC', 'XYZ', 'AABB', 'PPPP']
        }

df = pd.DataFrame(data)

print(df)

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

   values_1  values_2  values_3
0  5.521320  22.73520       AAA
1  6.572935  11.82000       ABC
2  7.210000  23.75839       XYZ
3  8.755000   4.22000      AABB
4  9.998900  15.11730      PPPP

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 approach to round the values under all the numeric columns:

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

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

import pandas as pd

data = {'values_1': [5.52132, 6.572935, 7.21, 8.755, 9.9989],
        'values_2': [22.7352, 11.82, 23.75839, 4.22, 15.1173],
        'values_3': ['AAA', 'ABC', 'XYZ', 'AABB', 'PPPP']
        }

df = pd.DataFrame(data)

df = df.round(decimals=2)

print(df)

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

   values_1  values_2  values_3
0      5.52     22.74       AAA
1      6.57     11.82       ABC
2      7.21     23.76       XYZ
3      8.76      4.22      AABB
4     10.00     15.12      PPPP

Alternatively, you can get the same results using NumPy:

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

So the complete Python code would look like this:

import pandas as pd
import numpy as np

data = {'values_1': [5.52132, 6.572935, 7.21, 8.755, 9.9989],
        'values_2': [22.7352, 11.82, 23.75839, 4.22, 15.1173],
        'values_3': ['AAA', 'ABC', 'XYZ', 'AABB', 'PPPP']
        }

df = pd.DataFrame(data)

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

print(df)

You’ll get the same results using NumPy:

   values_1  values_2  values_3
0      5.52     22.74       AAA
1      6.57     11.82       ABC
2      7.21     23.76       XYZ
3      8.76      4.22      AABB
4     10.00     15.12      PPPP

Leave a Comment