How do you print an entire Pandas DataFrame or Series without truncation? | A1Lab@MZeeshanM to PythonEnglish • 4 days ago

How do you print an entire Pandas DataFrame or Series without truncation?

message-square
0

Wrap your print() statement inside with pd.option_context("display.max_rows", None, "display.max_columns", None, "display.width", None, "display.max_colwidth", None): for a temporary, zero-side-effect full printout—or call print(df.to_string()) / print(s.to_string()) directly when you want every row, column, and full cell value printed in a single call without modifying global Pandas settings.

Basic Example

import pandas as pd

df = pd.DataFrame({
    f"col_{c}": [f"row_{r}_value_with_long_text" for r in range(100)]
    for c in range(20)
})
s = pd.Series([f"item_{i}_extended_description" for i in range(100)])

# 1. Recommended for one-off inspection: Temporary option_context (auto-resets after block)
with pd.option_context(
    "display.max_rows", None,
    "display.max_columns", None,
    "display.width", None,
    "display.max_colwidth", None,
):
    print(df)
    print(s)

# 2. Concise single-expression alternative (prints every row, column, and full cell text)
print(df.to_string())
print(s.to_string())

# 3. Set globally for an entire script or Jupyter notebook session
pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
pd.set_option("display.width", None)
pd.set_option("display.max_colwidth", None)

Key Details & Edge Cases

  • Why all four display options are needed for truly untruncated output:
    • display.max_rows = None: Prevents DataFrames or Series with more than 60 rows from collapsing middle rows into ....
    • display.max_columns = None: Prevents wide DataFrames from hiding middle columns with ... (ignored by Series, which has one column).
    • display.width = None: Prevents terminal scripts (python script.py) from wrapping wide DataFrames into stacked vertical blocks with \ continuation characters at 80 characters.
    • display.max_colwidth = None: Prevents long strings inside individual cells (over 50 characters) from being cut off with ....
  • Series-specific display.max_seq_items detail:
    • While display.max_rows = None controls pd.Series printing, printing raw df.index or df.columns objects separately is governed by display.max_seq_items (default 100). Set "display.max_seq_items", None if you also print huge Index objects without ....
  • Performance and memory warning on huge DataFrames (> 10,000 rows):
    • Calling df.to_string() or setting display.max_rows = None on a 500,000-row DataFrame formats every cell into one massive string in RAM—which can take several seconds and freeze a Jupyter notebook browser tab or flood your terminal scrollback buffer.
    • For large datasets, prefer inspecting targeted slices (print(df.head(200).to_string())) or writing to a temporary text/CSV file (df.to_csv("debug_dump.csv")) to view in an editor.
  • Jupyter Notebook HTML vs. plain-text print():
    • Evaluating df as the last expression in a Jupyter cell renders an HTML table that obeys display.max_rows, display.max_columns, and display.max_colwidth (while scrolling horizontally natively in CSS). Using print(df) inside Jupyter falls back to plain-text formatting, which does require display.width = None to avoid line wrapping.

Comparison of Untruncated Pandas Printing Approaches

Method Syntax Affects Global Settings? Works on Both DataFrame & Series? Best Used When
pd.option_context(...) with pd.option_context("display.max_rows", None, ...): print(df) No (auto-restores on exit) Yes Temporary full inspection inside scripts or functions without side effects.
.to_string() print(df.to_string()) / print(s.to_string()) No Yes Quick one-line full printout of small-to-medium DataFrames or Series.
pd.set_option(...) pd.set_option("display.max_rows", None) (etc.) Yes (session-wide) Yes Configuring a Jupyter notebook or exploratory script at the top of the file.
pd.reset_option(...) pd.reset_option("regex_pattern") Yes (reverts to default) Yes Restoring default Pandas truncation limits after global overrides.