5 Best Ways to Round Decimal Places in Pandas DataFrame Columns

πŸ’‘ Problem Formulation: When working with numerical data in pandas, you might need to round numbers to a certain number of decimal places for readability, consistency, or to meet a specification. For instance, you may have a DataFrame column with values like 3.14159265 and want to round them to two decimal places across the entire column, resulting in values like 3.14.

Method 1: Using the round() function

Pandas DataFrames have a built-in round() function tailored for rounding off numerical columns. This method is straightforward and provides a quick, in-place option to format your DataFrame values to the desired number of decimal places. The function has a single parameter – the number of decimal places you want to round to.

Here’s an example:

import pandas as pd

# Creating a DataFrame with floating point numbers
df = pd.DataFrame({'A': [1.2345, 2.4567, 3.7891], 'B': [4.2345, 5.6789, 6.9123]})
rounded_df = df.round(2)

print(rounded_df)

   A     B
0 1.23  4.23
1 2.46  5.68
2 3.79  6.91

In this snippet, the round() method is called on the entire dataframe df, rounding all floating point numbers in columns ‘A’ and ‘B’ to two decimal places. It’s simple, clean, and the syntax is very readable.

Method 2: Rounding Using the apply() function with lambda

Utilizing the apply() function with a lambda expression enables you to apply a custom rounding function to each value in your DataFrame columns. This method is flexible and can be useful when you need to perform more than just rounding operations.

Here’s an example:

rounded_df = df.apply(lambda x: round(x, 2))

print(rounded_df)

   A     B
0 1.23  4.23
1 2.46  5.68
2 3.79  6.91

The apply() function applies a lambda function that rounds each element in the DataFrame to two decimal places. The lambda function is a concise way to define a custom inline function, and apply() is very powerful for column-wise operations.

Method 3: Formatting with applymap() function

Pandas’ applymap() is used for element-wise operations across the entire DataFrame. Unlike apply(), which works on a series (column), applymap() works on each individual element. This method offers fine-grained control over the formatting of each cell in the DataFrame.

Here’s an example:

formatted_df = df.applymap(lambda x: f"{x:.2f}")

print(formatted_df)

      A     B
0  1.23  4.23
1  2.46  5.68
2  3.79  6.91

Here, each number is turned into a formatted string with two decimal places using Python’s f-string syntax within the applymap() function. Note, however, that the output is a DataFrame with string types rather than floats.

Method 4: Using the astype() function to specify decimal precision

You can also control the precision of float columns by using the astype() method. This approach doesn’t just round the number but also changes the underlying floating-point precision by converting it to a fixed-point precision which is often used when memory optimization is needed.

Here’s an example:

df['A'] = df['A'].astype('float64').round(2)
df['B'] = df['B'].astype('float64').round(2)

print(df)

   A     B
0 1.23  4.23
1 2.46  5.68
2 3.79  6.91

Each column is cast to a float with a specified decimal precision using astype('float64') before the rounding operation. This method ensures that the dtype of your columns is consistent and accurately represents the data within.

Bonus One-Liner Method 5: List Comprehension with Round

List comprehensions offer a Pythonic and concise way to apply rounding to columns. This method can be very fast for large datasets and enables you to apply different rounding rules to each column individually if needed.

Here’s an example:

df['A'] = [round(num, 2) for num in df['A']]
df['B'] = [round(num, 2) for num in df['B']]

print(df)

   A     B
0 1.23  4.23
1 2.46  5.68
2 3.79  6.91

The list comprehension iterates over each column and applies the round() function to each element, resulting in a rounded set of values. It’s elegant and fits well in a Pythonic codebase.

Summary/Discussion

  • Method 1: round() function. Simple and straightforward; works on entire DataFrame. Doesn’t work for series-specific rounding preferences.
  • Method 2: apply() function with lambda. Versatile and powerful for custom operations, but can be slower than other methods.
  • Method 3: applymap() function. Element-wise control and ideal for formatting, but results in string output that may need to be converted back to floats.
  • Method 4: astype() function. Good for memory optimization and ensuring precision, but requires additional code to set the dtype explicitly.
  • Method 5: List Comprehension with Round. Pythonic and very fast, but does not utilize pandas built-in vectorization capabilities, which could be a consideration for very large DataFrames.