Python Pandas - Options and Customization
Pandas provides a 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 customization's.
The primary functions available for these customization's are −
- get_option()
- set_option()
- reset_option()
- describe_option()
- option_context()
Frequently used Parameters
Before learning about the customization options, let's see about some of the frequently used Pandas display parameters that you can used for customization −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
display.max_rows Maximum number of rows to display. |
| 2 |
2 display.max_columns Maximum number of columns to display. |
| 3 |
display.expand_frame_repr Whether to expand the display of DataFrames across multiple lines. |
| 4 |
display.max_colwidth Maximum width of columns. |
| 5 |
display.precision Precision to display for decimal numbers. |
Let us now understand how the customization's functions operate.
Getting the Current Options
The get_option() function retrieves the current value of a specified parameter. This is useful for checking the current configuration of Pandas.
Example: Checking Maximum Rows Displayed
Following is the example that gets and returns the default number of maximum rows displayed. Interpreter reads this value and displays the rows with this value as upper limit to display.
import pandas as pd
print(pd.get_option("display.max_rows"))
Its output is as follows −
60
Example: Checking Maximum Columns Displayed
This example returns the default number of maximum columns displayed. Interpreter reads this value and displays the rows with this value as upper limit to display.
import pandas as pd
print(pd.get_option("display.max_columns"))
Its output is as follows −
0
Here, 60 and 0 are the default configuration parameter values.
Setting a New Option
The set_option() function allows you to change the value of a specific parameter, enabling you to customize how data is displayed.
Example: Changing Maximum Rows Displayed
Using set_option(), we can change the default number of rows to be displayed. Here is the example −
import pandas as pd
pd.set_option("display.max_rows",10)
print(pd.get_option("display.max_rows"))
Its output is as follows −
10
Example: Changing Maximum Columns Displayed
Following is the example that uses the set_option() function to change the default number of columns to be displayed.
import pandas as pd
pd.set_option("display.max_columns",30)
print(pd.get_option("display.max_columns"))
Its output is as follows −
30
Resetting an Option to Its Default Value
The reset_option() function resets the value of a specified parameter back to its default setting.
Example: Resetting Maximum Rows Displayed
Using the reset_option() function, we can change the value back to the default number of rows to be displayed.
import pandas as pd
pd.reset_option("display.max_rows")
print(pd.get_option("display.max_rows"))
Its output is as follows −
60
Describing an Option
The describe_option() function provides a description of a specified parameter, explaining what it does and its default value.
Example: Describing Maximum Rows Displayed
This example uses the reset_option() function to get the description of the max_row parameter.
import pandas as pd
pd.describe_option("display.max_rows")
Its output is as follows −
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]
Temporary Option Setting
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.
Example: Temporarily Changing Maximum Rows Displayed
This example uses the option_context() function to set the temporarily value for the maximum rows to displayed.
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"))
Its output is as follows −
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.