English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Custom Options in Pandas

Pandas Custom Option Operation Example

Pandas is widely used because it provides an API to customize behavior.
There are five related functions in the custom API as follows:

get_option()set_option()reset_option()describe_option()option_context()

Let's understand these methods together.

get_option(param)

get_option accepts a parameter and outputs the following values:

display.max_rows

Display the number of default values. The interpreter reads this value and displays the line as the upper limit.

 import pandas as pd
 print(pd.get_option("display.max_rows"))

Running Results:

    60

display.max_columns

Display the number of default values. The interpreter reads this value and displays the line as the upper limit.

 import pandas as pd
 print(pd.get_option("display.max_columns"))

Running Results:

    20

Here,60 and20 is the default configuration parameter value.

set_option(param, value)

set_option accepts two parameters and sets the value to the parameter, as shown below:

display.max_rows

Using set_option(), we can change the default number of rows to display.

 import pandas as pd
 pd.set_option("display.max_rows",80)
 print(pd.get_option("display.max_rows"))

Running Results:

    80

display.max_columns

Using set_option(), we can change the default number of rows to display.

 import pandas as pd
 pd.set_option("display.max_columns",30)
 print(pd.get_option("display.max_columns"))

Running Results:

    30

reset_option(param)

reset_option Accepts a parameter and sets it back to the default value.

display.max_rows

Using reset_option(), we can change the value back to the default number of rows to display.

 import pandas as pd
 pd.reset_option("display.max_rows")
 print(pd.get_option("display.max_rows"))

Running Results:

    60

describe_option(param)

describe_option Print the description of the parameters

display.max_rows

Using reset_option(), we can change the value back to the default number of rows to display.

 import pandas as pd
 pd.describe_option("display.max_rows")

Running Results:

    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]

option_context()

The option_context context manager is used to temporarily set options within the with statement. The option values are automatically restored when you exit the with block.

display.max_rows

Using option_context(), we can temporarily set values.

 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"))

Running Results:

    10
 10

See the difference between the first and second print statements. The first statement prints the value set by option_context(), which is temporary within the with context itself. After the with block, the second print statement prints the configured value.

Frequently Used Parameters

ParameterDescription
display.max_rowsDisplay Maximum Number of Rows to Show
display.max_columns<Display Maximum Number of Columns to Show
display.expand_frame_reprDisplay Data Frame to Stretch Page
display.max_colwidthDisplay Maximum Column Width
display.precisionDisplay Precision of Decimal Numbers