option_context()

Execute part of the code by different value of display options and restore the same after executing.
option_context()Execute the code block with set options ( reset after execution )
We can get a list of available options by using describe_option().

By using option_context() we can set the values of a set of display options temporarily and restore the values after execution of the set of code block. We will use with to define our set of code.

max_rows & max_columns

Let us set the option value max_rows to display 6 rows and max_columns to 3 columns of the DataFrame. Note that our excel file is having 35 rows of data and 5 columns.
import pandas as pd 
my_data = pd.read_excel('D:\student.xlsx',index_col='id')
with pd.option_context('display.max_rows',6,'display.max_columns',3):
    print(my_data)
print(pd.get_option("display.max_rows")) # 60 
Output
          name  ...     sex
id              ...        
1     John Deo  ...  female
2     Max Ruin  ...    male
3       Arnold  ...    male
..         ...  ...     ...
33   Kenn Rein  ...  female
34    Gain Toe  ...    male
35  Rows Noump  ...  female

[35 rows x 4 columns]
60
More on student excel file ( you can download and use ) having 35 rows of data

In above code we have set the value of max_rows to 6 and max_columns to 3. The print() command will display records based on this setting. After executing the print() command we will display the value of max_rows by using get_option(). The value ( output ) is restored to default value of 60.

Like this there are several options available and we can update the values of these function. A list of such options with detail description is available by using describe_option()
All display option functions of DataFrame

Pandas read_csv() read_excel() to_excel()
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer