Reset individual or selected display options, distinguish defaults from prior values and practise with portable Excel data.
Show Table of Contents ↓import pandas as pd
students = pd.DataFrame({"id": range(1, 36),
"name": [f"Student {i:02d}" for i in range(1, 36)],
"class": ["Four"] * 35, "mark": [40 + i for i in range(35)],
"sex": ["female" if i % 2 else "male" for i in range(35)]}).set_index("id")
print(students.head(3))
assert students.shape == (35, 4)Expected output
name class mark sex
id
1 Student 01 Four 40 male
2 Student 02 Four 41 female
3 Student 03 Four 42 malesaved = pd.get_option("display.max_rows")
with pd.option_context("display.max_rows", 6):
print("Changed:", pd.get_option("display.max_rows"))
returned = pd.reset_option("display.max_rows")
print("Reset default:", pd.get_option("display.max_rows"))
print(students.to_string(max_rows=35))
assert returned is None and students.shape == (35, 4)
print("Prior setting restored:", pd.get_option("display.max_rows") == saved)
assert pd.get_option("display.max_rows") == savedExpected output
Changed: 6
Reset default: 60
name class mark sex
id
1 Student 01 Four 40 male
2 Student 02 Four 41 female
3 Student 03 Four 42 male
4 Student 04 Four 43 female
5 Student 05 Four 44 male
6 Student 06 Four 45 female
7 Student 07 Four 46 male
8 Student 08 Four 47 female
9 Student 09 Four 48 male
10 Student 10 Four 49 female
11 Student 11 Four 50 male
12 Student 12 Four 51 female
13 Student 13 Four 52 male
14 Student 14 Four 53 female
15 Student 15 Four 54 male
16 Student 16 Four 55 female
17 Student 17 Four 56 male
18 Student 18 Four 57 female
19 Student 19 Four 58 male
20 Student 20 Four 59 female
21 Student 21 Four 60 male
22 Student 22 Four 61 female
23 Student 23 Four 62 male
24 Student 24 Four 63 female
25 Student 25 Four 64 male
26 Student 26 Four 65 female
27 Student 27 Four 66 male
28 Student 28 Four 67 female
29 Student 29 Four 68 male
30 Student 30 Four 69 female
31 Student 31 Four 70 male
32 Student 32 Four 71 female
33 Student 33 Four 72 male
34 Student 34 Four 73 female
35 Student 35 Four 74 male
Prior setting restored: Truebefore = pd.get_option("display.max_rows")
with pd.option_context("display.max_rows", 12):
with pd.option_context("display.max_rows", 6):
pd.reset_option("display.max_rows")
print("Registered default:", pd.get_option("display.max_rows"))
print("Previous custom value:", pd.get_option("display.max_rows"))
print("Outer restored:", pd.get_option("display.max_rows") == before)
assert pd.get_option("display.max_rows") == beforeExpected output
Registered default: 60
Previous custom value: 12
Outer restored: Truerows_before = pd.get_option("display.max_rows")
cols_before = pd.get_option("display.max_columns")
with pd.option_context("display.max_rows", 6, "display.max_columns", 2):
pd.reset_option(r"^display\.max_(rows|columns)$")
print("Row default:", pd.get_option("display.max_rows"))
print("Column default:", pd.get_option("display.max_columns"))
assert pd.get_option("display.max_rows") == rows_before
assert pd.get_option("display.max_columns") == cols_beforeExpected output
Row default: 60
Column default: 0import subprocess
import sys
worker = """import pandas as pd
pd.set_option('display.max_rows', 6)
pd.reset_option('display')
display_default = pd.get_option('display.max_rows')
assert display_default is not None
# Optional broad reset; may need plotting dependencies and emit warnings:
# pd.reset_option('all')
print('Display reset executed in isolated process')
"""
run = subprocess.run([sys.executable, "-c", worker], capture_output=True, text=True, check=True)
print(run.stdout.strip())
assert "reset executed" in run.stdoutExpected output
Display reset executed in isolated processfrom tempfile import TemporaryDirectory
from pathlib import Path
with TemporaryDirectory(dir=".") as folder:
workbook = Path(folder) / "student.xlsx"
students.reset_index().to_excel(workbook, index=False, engine="openpyxl")
loaded = pd.read_excel(workbook, index_col="id")
print("Imported shape:", loaded.shape)
pd.testing.assert_frame_equal(loaded, students)
print("Temporary workbook removed:", not workbook.exists())Expected output
Imported shape: (35, 4)
Temporary workbook removed: Trueprevious = pd.get_option("display.max_columns")
with pd.option_context("display.max_columns", 2):
pd.reset_option("display.max_columns")
print("Reset columns:", pd.get_option("display.max_columns"))
print("Prior value restored:", pd.get_option("display.max_columns") == previous)
assert pd.get_option("display.max_columns") == previousExpected output
Reset columns: 0
Prior value restored: TrueOpen in Google Colab View on GitHub
Run examples in order, inspect the table display and try the exercise. Save a copy to keep edits.
Reference: Pandas reset_option() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.