Table of Contents
TogglePandas provides an API to customize various aspects of its behavior, particularly related to display settings. This customization is essential for adjusting how data is presented based on your needs. Whether you want to adjust how many rows and columns are displayed or change the precision of floating-point numbers, Pandas provides a flexible and powerful API for these customizations.
The primary functions available for these customizations are −
Before learning about the customization options, let’s see about some of the frequently used Pandas display parameters that you can use for customization −
| Sr.No | Parameter & Description |
|---|---|
| 1 | display.max_rowsMaximum number of rows to display. |
| 2 | display.max_columnsMaximum number of columns to display. |
| 3 | display.expand_frame_reprWhether to expand the display of DataFrames across multiple lines. |
| 4 | display.max_colwidthMaximum width of columns. |
| 5 | display.precisionPrecision to display for decimal numbers. |
The get_option() function retrieves the current value of a specified parameter. This is useful for checking the current configuration of Pandas.
import pandas as pd
print(pd.get_option("display.max_rows"))
Output:
60
import pandas as pd
print(pd.get_option("display.max_columns"))
Output:
0
The set_option() function allows you to change the value of a specific parameter, enabling you to customize how data is displayed.
import pandas as pd
pd.set_option("display.max_rows",10)
print(pd.get_option("display.max_rows"))
Output:
10
import pandas as pd
pd.set_option("display.max_columns",30)
print(pd.get_option("display.max_columns"))
Output:
30
The reset_option() function resets the value of a specified parameter back to its default setting.
import pandas as pd
pd.reset_option("display.max_rows")
print(pd.get_option("display.max_rows"))
Output:
60
The describe_option() function provides a description of a specified parameter, explaining what it does and its default value.
import pandas as pd
pd.describe_option("display.max_rows")
Output:
display.max_rows : int If max_rows is exceeded, switch to truncate view. Depending on 'large_repr', objects are either centrally truncated or printed as a summary view. 'None' value means unlimited. In case python/IPython is running in a terminal and `large_repr` equals 'truncate' this can be set to 0 and pandas will auto-detect the height of the terminal and print a truncated object which fits the screen height. The IPython notebook, IPython qtconsole, or IDLE do not run in a terminal and hence it is not possible to do correct auto-detection. [default: 60] [currently: 60]
The option_context() function allows you to set an option temporarily within a with statement. Once the context is exited, the option is automatically reverted to its previous value.
import pandas as pd
with pd.option_context("display.max_rows",10):
print(pd.get_option("display.max_rows"))
print(pd.get_option("display.max_rows"))
Output:
10 60
See, the difference between the first and the second print statements. The first statement prints the value set by option_context() which is temporary within the with context itself. After the with context, the second print statement prints the configured value.
