pandas
python
dataframes
pretty printing
data visualization

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.

Practice ML system design

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:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Alice", "Bob"],
5    "score": [91.2345, 88.5],
6})
7
8print(df.to_string(index=False))

Output:

text
 name   score
Alice 91.2345
  Bob 88.5000

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.

python
1import pandas as pd
2
3with pd.option_context(
4    "display.max_rows", None,
5    "display.max_columns", None,
6    "display.width", 120,
7):
8    print(df)

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:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "item": ["A", "B", "C"],
5    "price": [12.5, 9.0, 103.45678],
6})
7
8print(df.to_string(
9    index=False,
10    formatters={"price": lambda x: f"${x:,.2f}"}
11))

Output:

text
1item   price
2   A  $12.50
3   B   $9.00
4   C $103.46

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:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Alice", "Bob", "Cara"],
5    "score": [91, 88, 95],
6})
7
8styled = (
9    df.style
10      .format({"score": "{:.0f}"})
11      .highlight_max(subset=["score"], color="#d9f2d9")
12)
13
14styled

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:

python
print(df.to_markdown(index=False))

HTML:

python
html = df.to_html(index=False)
print(html[:120])

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.

python
1import pandas as pd
2
3pd.set_option("display.max_colwidth", 60)
4pd.set_option("display.width", 140)

For temporary use, prefer option_context:

python
with pd.option_context("display.max_colwidth", 60, "display.width", 140):
    print(df)

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()) or print(df.to_string()) for debugging'
  • 'Styler, to_markdown(), or to_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 Styler in 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
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.