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.
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.