Pretty Printing a pandas dataframe
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
Pretty-printing a pandas DataFrame is really about choosing the right display format for the environment you are in. The best output for a terminal, a notebook, a markdown report, and an HTML export are not the same. Pandas gives you several ways to control formatting, truncation, alignment, and styling once you separate "display for humans" from "data for computation."
Start with to_string() for Plain Text
If you want reliable terminal-friendly output, to_string() is a strong default:
Output:
print(df) is fine for quick exploration, but to_string() gives you more control and behaves more predictably in scripts and logs.
Control Truncation with Display Options
Pandas truncates rows and columns when the DataFrame is large. For debugging or reporting, you may want to override that behavior temporarily.
Using option_context is better than changing global options permanently because the formatting changes stay local to one block of code.
This is especially useful in shared notebooks or long-running sessions where global display state can become confusing.
Format Numeric Columns Cleanly
Raw floating-point output is often noisy. You can control it with a formatter:
Output:
That is much better for human consumption than raw floating-point values.
Pretty Printing in Notebooks with Styler
In Jupyter or other HTML-capable environments, Styler is often the nicest way to present a DataFrame:
This does not change the underlying data. It only changes presentation.
That distinction matters because Styler is for notebooks, reports, and HTML export, not for ordinary terminal output.
Export-Friendly Formats
Sometimes "pretty print" really means "render it in a format I can paste into documentation." Pandas supports several output methods for that.
Markdown:
HTML:
CSV is not pretty printing, but it is worth mentioning because developers sometimes use it accidentally when what they really wanted was human-readable output.
If the destination is a README, issue comment, or documentation page, to_markdown() is often the best fit.
Column Width and Wrapping
Long text columns are where default DataFrame display often becomes ugly. You can improve readability by expanding display width or preprocessing the text.
For temporary use, prefer option_context:
This keeps output readable without permanently changing the whole session.
Separate Debug Output from Presentation Output
A good habit is to treat debug printing and polished presentation as different tasks.
Use:
- '
print(df.head())orprint(df.to_string())for debugging' - '
Styler,to_markdown(), orto_html()for presentation'
Mixing those concerns often leads to awkward code that is neither good for logs nor good for final presentation.
For example, Styler objects do not display meaningfully in plain terminal logs, and raw print(df) is rarely ideal for documentation output.
Common Pitfalls
The biggest mistake is changing global pandas display options and forgetting to reset them. That can make later output confusing in the same session.
Another issue is using notebook-only styling tools and expecting them to look nice in plain text terminals. Styler is for HTML-rendered environments, not standard console output.
Developers also often forget that pretty printing should not mutate the data itself. Formatting should happen at display time, not by converting numeric columns into strings prematurely.
Finally, print(df) is fine for quick inspection, but when layout matters, use to_string(), to_markdown(), or Styler intentionally instead of hoping the default repr is good enough.
Summary
- Use
to_string()for predictable plain-text DataFrame output. - Use
pd.option_context(...)to control truncation and width without changing global settings permanently. - Apply numeric formatting at display time for cleaner output.
- Use
Stylerin notebook or HTML contexts, not as a general terminal solution. - Choose the output format based on the destination: console, markdown, HTML, or report.
Related reading
- Principal Component Analysis in MATLAB
- Principal Component Analysis PCA on huge sparse dataset
- Principle of setting 'hash_bucket_size' parameter?
- Print top 10 users who post lots of questions and lots of answers of a Web Forum
- Pretty printing XML in Python
- Print a list in reverse order with range?
- Print very long string in Pandas dataframe
- Printing Lists as Tabular Data
.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.