How to Round Values in a pandas DataFrame

In this tutorial, you will learn how to round values in a DataFrame.

TLDR solution

# round to x number of decimals in one column
df['column'].round(x)

# entire df
df.round(x)

Example 1: Round Values in a Column

Let's say, you have the following DataFrame:

import pandas as pd


data = {'fish': ['salmon', 'pufferfish', 'shark'],
        'length_m': [1.523, 0.2165, 2.1],
        'width_cm': [10.2, 3.14159, 90.0]
        }   

df = pd.DataFrame(data)

print(df)
         fish  length_m  width_cm
0      salmon    1.5230  10.20000
1  pufferfish    0.2165   3.14159
2       shark    2.1000  90.00000

To round the the length_m column to two decimals places, run the following:

df['length_m'] = df['length_m'].round(2)

print(df['length_m'])
0    1.52
1    0.22
2    2.10
Name: length_m, dtype: float64

Example 2: Round Values in the Entire DataFrame

You can also round all numerical values in a DataFrame at once:

import pandas as pd


data = {'fish': ['salmon', 'pufferfish', 'shark'],
        'length_m': [1.523, 0.2165, 2.1],
        'width_cm': [10.2, 3.14159, 90.0]
        }   

df = pd.DataFrame(data)

df = df.round(2)

print(df)
         fish  length_m  width_cm
0      salmon      1.52     10.20
1  pufferfish      0.22      3.14
2       shark      2.10     90.00

That's it! You just rounded values in a DataFrame.