Styling & Formatting Output
Output formatting is the set of tools that control how a DataFrame looks when you print or export it — adjusting display options, rounding numbers, emitting Markdown, and adding colour with the Styler — without ever changing the underlying data.
Learn Styling & Formatting Output in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You will tune pd.options.display , round and format numbers, export with to_markdown() and to_string() , and apply the .style API's format , background_gradient , and highlight_max for notebooks.
When pandas prints a large frame it truncates with ... . pd.set_option() raises those limits, and a global display.float_format controls decimal places everywhere. None of this touches the data — it only changes what you see. For a one-off, .round(n) returns a rounded copy.
to_string() renders the whole DataFrame as plain text with no truncation — great for logs and console scripts. to_markdown() produces a Markdown table you can paste into GitHub, docs, or a chat message. Both return a string, so you can print it or write it to a file.
df.style returns a Styler that renders rich HTML in Jupyter and browsers. .format() sets per-column number formatting, .background_gradient() shades cells by value, and .highlight_max() marks the largest value in each column. These affect presentation only — the data is untouched.
✅ Fix: in plain text, round and use to_string:
Lesson complete — your tables look professional!
You can tune display options, round and format numbers, export with to_markdown() and to_string() , and dress up notebook tables with the .style API — all without altering the data.
🚀 Up next: Checkpoint — Analysis & Aggregation — put the whole analysis toolkit together.
Practice quiz
Which call stops pandas truncating rows when it prints?
- pd.show_all_rows()
- pd.set_option('display.max_rows', None)
- df.expand()
- pd.no_truncate()
Answer: pd.set_option('display.max_rows', None). Setting display.max_rows to None tells pandas to print every row.
What does df.round(2) return?
- None
- The original frame modified in place
- A rounded copy of the DataFrame
- Only the rounded column
Answer: A rounded copy of the DataFrame. round returns a new rounded copy; the original is unchanged.
Which method renders the whole DataFrame as plain text with no truncation?
- df.to_markdown()
- df.to_html()
- df.show()
- df.to_string()
Answer: df.to_string(). to_string() dumps the full table as untruncated plain text.
Which method produces a Markdown table you can paste into GitHub?
- df.to_markdown()
- df.to_text()
- df.to_md()
- df.markdown()
Answer: df.to_markdown(). to_markdown() emits a Markdown-formatted table (needs tabulate).
What does df.style return?
- A plain string
- A Styler object that renders HTML
- A new DataFrame
- A list of colours
Answer: A Styler object that renders HTML. df.style returns a Styler that renders rich HTML in notebooks.
Where do .style colours actually render?
- In a plain terminal
- In log files
- In HTML environments like Jupyter and browsers
- In CSV exports
Answer: In HTML environments like Jupyter and browsers. The Styler needs an HTML environment to show colours.
Does the .style API change the underlying data?
- Yes, it rounds the values
- Yes, it drops columns
- Only for numeric columns
- No, it only affects presentation
Answer: No, it only affects presentation. Styling affects the visual HTML only; the data is untouched.
Which Styler method shades cells by their value?
- background_gradient()
- color_scale()
- shade_by()
- heat()
Answer: background_gradient(). .background_gradient() shades each cell based on its value.
Which package does to_markdown() rely on?
- numpy
- openpyxl
- tabulate
- jinja2
Answer: tabulate. to_markdown() needs the tabulate package installed.
How do you set a global 2-decimal display for floats?
- df.round('all')
- pd.decimals(2)
- df.format(2)
- pd.set_option('display.float_format', '{:.2f}'.format)
Answer: pd.set_option('display.float_format', '{:.2f}'.format). display.float_format controls how floats print everywhere.