How to Round a Single Column in Pandas DataFrame

To round a single column in a Pandas DataFrame, you can use the round() method on the column, specifying the number of decimal places to round to. This will apply the rounding to all the values in the column, and update the column in the DataFrame with the rounded values. You can then use the Pandas DataFrame to display or export the rounded values.


You can use the following basic syntax to round the values in a single column of a pandas DataFrame:

df.my_column = df.my_column.round()

The following example shows how to use this syntax in practice.

Example: Round a Single Column in Pandas DataFrame

Suppose we have the following pandas DataFrame that contains information about various athletes:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'athlete': ['A', 'B', 'C', 'D', 'E', 'F'],
                   'time': [12.443, 15.8, 16.009, 5.06, 11.075, 12.9546],
                   'points': [5, 7, 7, 9, 12, 9]})

#view DataFrame
print(df)

  athlete     time  points
0       A  12.4430       5
1       B  15.8000       7
2       C  16.0090       7
3       D   5.0600       9
4       E  11.0750      12
5       F  12.9546       9

We can use the following code to round each value in the time column to the nearest integer:

#round values in 'time' column of DataFrame
df.time = df.time.round()

#view updated DataFrame
print(df)

  athlete  time  points
0       A  12.0       5
1       B  16.0       7
2       C  16.0       7
3       D   5.0       9
4       E  11.0      12
5       F  13.0       9

Each value in the time column has been rounded to the nearest integer.

For example:

  • 12.443 has been rounded to 12.
  • 15.8 has been rounded to 16.
  • 16.009 has been rounded to 16.

And so on.

To round the values in a column to a specific number of decimal places, simply specify that value in the round() function.

For example, we can use the following code to round each value in the time column to two decimal places:

#round values in 'time' column to two decimal places
df.time = df.time.round(2)

#view updated DataFrame
print(df)

  athlete   time  points
0       A  12.44       5
1       B  15.80       7
2       C  16.01       7
3       D   5.06       9
4       E  11.08      12
5       F  12.95       9

Each value in the time column has been rounded to two decimal places.

For example:

  • 12.443 has been rounded to 12.44.
  • 15.8 has been rounded to 15.80.
  • 16.009 has been rounded to 1601.

And so on.

Also note that the values in the other numeric column, points, have remained unchanged.

x