Pandas DataFrame.round() method: Explained with examples

Updated: February 20, 2024 By: Guest Contributor Post a comment

Overview

Pandas is a fast, powerful, flexible, and easy to use open-source data analysis and manipulation tool, built on top of the Python programming language. One of the essential methods when it comes to data cleaning and preparation in Pandas is the .round() method. This method is used to round a DataFrame’s numeric values to a specified number of decimal places. This tutorial will guide you through the round() method in Pandas, illustrated with examples ranging from basic to advanced usage.

The Basics of the round() Method

The round() method in Pandas is straightforward to use. It rounds the values in your DataFrame to a specified number of decimal places and is particularly useful in data cleaning processes where precision of decimal places needs to be standardized across your dataset.

import pandas as pd 

# Sample DataFrame
data = {'Score': [4.658, 3.142, 5.667], 'Rating': [2.456, 1.347, 5.972]}
df = pd.DataFrame(data)

# Rounding to 1 decimal place
df_rounded = df.round(1)
print(df_rounded)

This would output:

   Score  Rating
0    4.7     2.5
1    3.1     1.3
2    5.7     6.0

Rounding Specific Columns

You might not always want to round all the columns in your DataFrame. Pandas allows you to specify exactly which columns to round and by how many decimal places.

df_rounded = df.round({'Score': 0, 'Rating': 1})
print(df_rounded)

This outputs:

   Score  Rating
0    5.0     2.5
1    3.0     1.3
2    6.0     6.0

Handling Non-Numeric Columns

If your DataFrame contains non-numeric columns, the round() method will simply ignore these columns and round only the numeric ones. Here’s an example:

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Score': [4.658, 3.142, 5.667], 'Rating': [2.456, 1.347, 5.972]}
df = pd.DataFrame(data)

# Rounding to 2 decimal places
df_rounded = df.round(2)
print(df_rounded)

This would output:

      Name  Score  Rating
0    Alice   4.66    2.46
1      Bob   3.14    1.35
2  Charlie   5.67    5.97

Advanced Usage

For more complex scenarios, such as when working with multi-level column indexes or needing to apply different rounding rules within the same DataFrame, the round() method can still be effectively utilized. Here’s an example:

arrays = [['A', 'A', 'B', 'B'], ['One', 'Two', 'One', 'Two']]
tuples = list(zip(*arrays))
col_index = pd.MultiIndex.from_tuples(tuples)
data = [[1.234, 2.345, 3.456, 4.567], [5.678, 6.789, 7.890, 8.901]]
df = pd.DataFrame(data, columns=col_index)

# Rounding the entire DataFrame
df_rounded = df.round(2)
print(df_rounded)

This outputs:

     A           B      
   One   Two   One  Two
0 1.23  2.35  3.46 4.57
1 5.68  6.79  7.89 8.90

Conclusion

This tutorial aimed to provide a comprehensive understanding of the round() method in Pandas, covering its basic to advanced applications with examples. Understanding and utilizing this method can significantly aid in data cleaning and preparation, ensuring numerical data is consistently formatted throughout your datasets.