Uniform report formatting
Prepare tabular numeric output for reports by applying one consistent decimal precision.
Pandas · Module
Rounds numeric DataFrame columns to a chosen precision. Use an integer for uniform precision or a dict/Series for per-column precision.
Call round with an integer to apply one precision to every numeric column, or pass a dictionary or Series to choose precision per column. The operation returns a DataFrame; columns omitted from a mapping remain unchanged, and non-numeric columns are not rounded.
DataFrame.round(decimals=0, *args, **kwargs)| Name | Type | Description |
|---|---|---|
decimals | int, dict, or Series | Number of decimal places for each column. An integer applies uniformly; a dict or Series specifies per-column precision. |
**args, **kwargs | optional | Additional keyword arguments accepted for compatibility with NumPy; they have no effect. |
DataFrame with the affected columns rounded to the specified number of decimal places.
import pandas as pd
df = pd.DataFrame({
"price": [1.234, 5.678],
"quantity": [2.345, 6.789],
"label": ["a", "b"],
})
rounded = df.round({"price": 2, "quantity": 1})
print(rounded)Rounds selected numeric columns independently while leaving the text column unchanged.
Prepare tabular numeric output for reports by applying one consistent decimal precision.
Round measurements, prices, or ratios with different precision requirements per column.
Create a presentation-ready copy while preserving the original unrounded DataFrame.
An integer applies the same number of decimal places to every numeric column. A dict maps column names to individual precisions, while a Series uses its index as the column-name mapping.
Follow-up: What happens to columns omitted from a dict? They remain unchanged, so a mapping can target only the columns requiring a different precision.
The method returns a DataFrame containing the affected columns rounded to the requested precision. It does not modify the original DataFrame in place.
Follow-up: How would you preserve the original while using the result? Assign the returned DataFrame to another variable, as in rounded = df.round(2).
The decimals argument accepts an integer, dictionary, or Series. Dictionary keys must be column names; for a Series, the relevant column names must be in its index.
Follow-up: What happens if a mapping includes a name absent from the DataFrame? That entry is ignored.
DataFrame.absDataFrame.clipDataFrame.astype