Sling Academy
Home/Pandas/Pandas DataFrame.round() method: Explained with examples

Pandas DataFrame.round() method: Explained with examples

Last updated: February 20, 2024

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.

Next Article: Pandas – DataFrame.sem() method (3 examples)

Previous Article: Computing data ranks in Pandas DataFrame (5 examples)

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)