Set pd.set_option("display.max_columns", None) (and pd.set_option("display.width", None) or 1000 in terminal scripts so wide tables do not wrap awkwardly across lines), or wrap a single print statement in the temporary with pd.option_context("display.max_columns", None, "display.width", None): context manager so global display settings remain unchanged.
Basic Example
import pandas as pd
# Create a wide DataFrame with 30 columns
df = pd.DataFrame([[i for i in range(30)]], columns=[f"col_{i}" for i in range(30)])
# 1. Recommended for temporary / one-off inspection (resets automatically after block)
with pd.option_context("display.max_columns", None, "display.width", None):
print(df)
# 2. Set globally for the entire script or Jupyter notebook session
pd.set_option("display.max_columns", None)
pd.set_option("display.width", None) # Prevents terminal line-wrapping
pd.set_option("display.max_colwidth", None) # Optional: prevents truncating long cell strings
print(df)
# 3. Restore defaults at any time
pd.reset_option("display.max_columns")
Key Details & Edge Cases
- Why terminal output still wraps onto multiple blocks without
display.width:- Setting
display.max_columns = Nonestops Pandas from replacing middle columns with, but in a standard terminal (python script.py), Pandas still checksdisplay.width(default80characters) and splits wide tables into stacked vertical blocks with\continuation markers. - Setting
pd.set_option("display.width", None)(or a large integer like1000) tells Pandas to auto-detect or expand the terminal width so all columns render side-by-side horizontally. (In Jupyter notebooks, HTML rendering ignoresdisplay.widthand scrolls horizontally automatically.)
- Setting
- Preventing long text inside individual cells from truncating (
display.max_colwidth):- Even with all columns shown, Pandas truncates long strings inside a cell at 50 characters with
. Setpd.set_option("display.max_colwidth", None)when inspecting URLs, JSON strings, or full text fields.
- Even with all columns shown, Pandas truncates long strings inside a cell at 50 characters with
- Showing all rows as well (
display.max_rows):- By default, DataFrames with more than 60 rows collapse middle rows to 10 rows (
display.min_rows = 10). Setpd.set_option("display.max_rows", None)alongsidedisplay.max_columnsif you want every row printed—though avoid doing this on multi-thousand-row DataFrames in Jupyter, as rendering huge HTML tables can freeze the browser tab.
- By default, DataFrames with more than 60 rows collapse middle rows to 10 rows (
df.to_string()as a zero-config one-liner for scripts:- Calling
print(df.to_string())prints all rows, all columns, and full cell widths in a single call without modifying any global Pandas options (useprint(df.head(10).to_string())to avoid dumping millions of rows).
- Calling
Comparison of Pandas Display Configuration Methods
| Method / Option | Syntax | Scope | Prevents Line-Wrapping & ? |
Best Used When |
|---|---|---|---|---|
pd.set_option("display.max_columns", None) |
pd.set_option("display.max_columns", None) |
Global session | Shows all columns (pair with display.width = None) |
Interactive Jupyter notebooks or exploratory analysis scripts. |
pd.option_context(...) |
with pd.option_context("display.max_columns", None): |
Temporary (with block) |
Yes, inside the with block only |
Inspecting a specific wide DataFrame cleanly without mutating global settings. |
df.to_string() |
print(df.head().to_string()) |
Single expression | Yes (shows all columns, rows, and full text) | Quick console debugging or logging a small DataFrame without touching pd.options. |
display.max_colwidth |
pd.set_option("display.max_colwidth", None) |
Global / context | Prevents inside wide text cells |
Viewing long strings, file paths, or error messages stored inside DataFrame cells. |
pd.reset_optio("all") |
pd.reset_optio("display.max_columns") |
Global session | Restores Pandas default truncation limits | Reverting custom display overrides back to standard Pandas defaults. |
You must log in or register to comment.
