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.
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:
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:
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:
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:
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:
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_contextfor temporary display changes - use
roundfor 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_formatand forgetting that it changes unrelated output later in the session. - Expecting
roundalone 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
roundwhen 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
- Format y axis as percent
- Frequency counts for unique values in a NumPy array
- Fuel chart smoothing algorithm
- Future prediction using time series data set with Tensorflow
- Format numbers in django templates
- Format timedelta to string
- FutureWarning arrays to stack must be passed as a sequence type such as list or tuple. Support for non-sequence iterables is deprecated
- FutureWarning Conversion of the second argument of issubdtype from float to np.floating is deprecated
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.