Read current Pandas display options, set row and column limits and restore prior settings safely.
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 maleprint("Current max_rows:", pd.get_option("display.max_rows"))
print("Current max_columns:", pd.get_option("display.max_columns"))
assert pd.get_option("display.max_rows") is None or pd.get_option("display.max_rows") >= 0Expected output
Current max_rows: 60
Current max_columns: 0previous = pd.get_option("display.max_rows")
try:
returned = pd.set_option("display.max_rows", 6)
print("Changed max_rows:", pd.get_option("display.max_rows"))
with pd.option_context("display.min_rows", 6, "display.max_columns", 4,
"display.width", 80, "display.show_dimensions", True):
print(students)
assert returned is None and students.shape == (35, 4)
finally:
pd.set_option("display.max_rows", previous)
assert pd.get_option("display.max_rows") == previousExpected output
Changed max_rows: 6
name class mark sex
id
1 Student 01 Four 40 male
2 Student 02 Four 41 female
3 Student 03 Four 42 male
.. ... ... ... ...
33 Student 33 Four 72 male
34 Student 34 Four 73 female
35 Student 35 Four 74 male
[35 rows x 4 columns]old_rows = pd.get_option("display.max_rows")
old_columns = pd.get_option("display.max_columns")
try:
pd.set_option("display.max_rows", 6, "display.max_columns", 3)
with pd.option_context("display.min_rows", 6, "display.width", 80,
"display.show_dimensions", True):
print(students)
assert pd.get_option("display.max_columns") == 3
finally:
pd.set_option("display.max_rows", old_rows, "display.max_columns", old_columns)
assert students.shape == (35, 4)Expected output
name ... sex
id ...
1 Student 01 ... male
2 Student 02 ... female
3 Student 03 ... male
.. ... ... ...
33 Student 33 ... male
34 Student 34 ... female
35 Student 35 ... male
[35 rows x 4 columns]from 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: Truesaved = pd.get_option("display.max_rows")
with pd.option_context("display.max_rows", 10):
print("Temporary max_rows:", pd.get_option("display.max_rows"))
print("Previous value restored:", pd.get_option("display.max_rows") == saved)
assert pd.get_option("display.max_rows") == savedExpected output
Temporary max_rows: 10
Previous value restored: Truetry:
pd.get_option("display.this_option_does_not_exist")
except pd.errors.OptionError:
print("Unknown option rejected")
else:
raise AssertionError("Expected OptionError")
saved = pd.get_option("display.max_rows")
try:
pd.set_option("display.max_rows", "six")
except ValueError:
print("Invalid value rejected")
else:
raise AssertionError("Expected ValueError")
assert pd.get_option("display.max_rows") == savedExpected output
Unknown option rejected
Invalid value rejectedsaved = pd.get_option("display.precision")
try:
pd.set_option("display.precision", 2)
print("Precision:", pd.get_option("display.precision"))
assert pd.get_option("display.precision") == 2
finally:
pd.set_option("display.precision", saved)
print("Restored:", pd.get_option("display.precision") == saved)Expected output
Precision: 2
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 get_option() and set_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.