Python Pandas – Options and Customization

Pandas provide customization API for its behaviour and display. The customization API are:

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

 

Let’s see how these function works:

get_option: Retrieves the value of the specified option.

pandas.get_option(option)
In [1]: import pandas as pd
 
In [2]: pd.get_option("display.max_rows")
Out[2]: 60
 
In [3]: pd.get_option("display.max_columns")
Out[3]: 20
 
In [4]: pd.get_option("display.max_colwidth")
Out[4]: 50
 
In [5]: pd.get_option("display.width")
Out[5]: 80

.     .     .

Set_option: Sets the value of the specified option.

pandas.set_option(option, value)

Example

In [1]: import pandas as pd
 
In [2]: pd.set_option("display.max_rows",80)
In [3]: pd.set_option("display.max_columns",30)

.     .     .

reset_option: Reset one or more options to their default value. Pass “all” as an argument to reset all options.

pandas.reset_option(option)

Example

In [1]: import pandas as pd
 
In [2]: pd.reset_option("display.max_rows")
In [3]: pd.reset_option("display.max_columns")

.     .     .

describe_option : Prints the description for one or more registered options. Call with not arguments to get a listing for all registered options.

pandas.describe_option(option, _print_desc=False)
In [1]: import pandas as pd 
In [2]: pd.describe_option("display.max_columns")
Out[2]: 
display.max_columns : int
    If max_cols 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 width of the terminal and print a truncated object which fits
    the screen width. 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: 20] [currently: 20]

.     .     .

option_context : Context manager to temporarily set options in the with statement context.

option_context(option, val, [(option, val), ...])

Example:

import pandas as pd
with pd.option_context("display.max_columns",10):
    print("'with' block - Inner : ",pd.get_option("display.max_columns"))
print("'with' block - Outer : ",pd.get_option("display.max_columns"))

Output:
 
'with' block - Inner :  10
'with' block - Outer :  20

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Pandas Tutorials