pandas
python
data-analysis
scientific-notation
data-formatting

Format / Suppress Scientific Notation from Pandas Aggregation Results

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Pandas often displays very large or very small aggregation results in scientific notation because that is compact and numerically sensible. The important detail is that this is usually a display choice, not a change to the underlying numeric values.

That distinction matters because there are two different tasks people mix together: changing how the result is shown, and converting the result into formatted text for export or reporting. In most cases, you should keep the result numeric as long as possible and apply formatting only at the presentation step.

Format Aggregation Output for Display

If you only want to change what you see when printing a result, use a display option or an option context. Here is a simple example with a grouped aggregation:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "team": ["A", "A", "B", "B"],
6        "amount": [2.5e8, 3.75e8, 4.1e8, 5.2e8],
7    }
8)
9
10result = df.groupby("team")["amount"].sum()
11
12print(result)
13
14with pd.option_context("display.float_format", "{:,.2f}".format):
15    print(result)

Inside the option_context, Pandas prints values such as 625,000,000.00 instead of 6.250000e+08. Once the context exits, the display setting goes back to its previous state.

This is usually the best approach for notebooks, debugging, or console output because the data remains numeric.

Use round When You Want Numeric Rounding

If you want fewer decimals but still need the result to remain numeric, use round:

python
1import pandas as pd
2
3df = pd.DataFrame({"group": ["A", "A", "B"], "value": [0.000012345, 0.000067891, 0.000034567]})
4result = df.groupby("group")["value"].mean().round(8)
5
6print(result)

round changes the numeric values themselves. It does not guarantee that Pandas will stop using scientific notation in every display context, but it is appropriate when the rounded value is actually what you want to keep.

That is different from pure formatting, which only changes presentation.

Convert to Strings Only at the Final Presentation Step

If the aggregation result is going into a report, CSV export, or UI layer as text, format it explicitly:

python
1import pandas as pd
2
3df = pd.DataFrame({"group": ["A", "A", "B"], "value": [2.5e8, 3.75e8, 4.1e8]})
4result = df.groupby("group")["value"].sum()
5
6formatted = result.map(lambda value: f"{value:,.0f}")
7print(formatted)

This produces string values such as "625,000,000" rather than floats. That is useful for presentation, but it also means you should not expect to do more math on formatted without converting back.

For DataFrames, the same idea works with style.format in notebooks:

python
summary = df.groupby("group", as_index=False)["value"].sum()
styled = summary.style.format({"value": "{:,.0f}"})

This leaves the DataFrame numeric while controlling how it is rendered in supported frontends.

Prefer Local Formatting Over Global Session Settings

You can set a global display option:

python
pd.options.display.float_format = "{:,.4f}".format

That works, but it affects later output in the same session, which can be surprising in notebooks or shared analysis code. A local option context is safer when you only want to suppress scientific notation for one print or one cell.

A useful rule is:

  • use option_context for temporary display changes
  • use round for numeric rounding
  • use string formatting only for final presentation

Common Pitfalls

  • Converting aggregation results to strings too early, then discovering later code can no longer perform numeric operations on them.
  • Using global pd.options.display.float_format and forgetting that it changes unrelated output later in the session.
  • Expecting round alone to control every display choice. It changes values, not all formatting behavior.
  • Formatting only the raw column and forgetting that the grouped aggregation result may still display differently.
  • Confusing notebook rendering with exported values. A nicely formatted notebook view does not automatically change the CSV output.

Summary

  • Scientific notation in Pandas aggregation results is usually a display issue, not a data issue.
  • Use pd.option_context("display.float_format", ...) when you only want prettier printed output.
  • Use round when you want to change the numeric values themselves.
  • Convert to strings only when you are preparing the final presentation layer.
  • Prefer local formatting over global session settings so you do not affect unrelated analysis output.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.