How can I round a single column in a Pandas DataFrame?

How can I round a single column in a Pandas DataFrame?

The process of rounding a single column in a Pandas DataFrame involves using the built-in “round” function and specifying the column name as the parameter. This function will round all values in the specified column to the desired number of decimal places. This method is useful for data analysis and presentation purposes, as it allows for cleaner and more concise data representation. Additionally, it can be easily applied to multiple columns or the entire DataFrame.

Round a Single Column in Pandas DataFrame


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.

The following tutorials explain how to perform other common operations in pandas:

Cite this article

stats writer (2024). How can I round a single column in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-round-a-single-column-in-a-pandas-dataframe/

stats writer. "How can I round a single column in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-round-a-single-column-in-a-pandas-dataframe/.

stats writer. "How can I round a single column in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-round-a-single-column-in-a-pandas-dataframe/.

stats writer (2024) 'How can I round a single column in a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-round-a-single-column-in-a-pandas-dataframe/.

[1] stats writer, "How can I round a single column in a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I round a single column in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top