Options and Customization in Python Pandas

Divya Bhat

a year ago

In this article, we will see how we can customize our data using different functions provided by the pandas API.
Options and Customization in Python
Options and Customization in Python
Pandas provides some default functions which restrict the analysis of the data. So, to customize your data, it is important to know the various functions available to change the default pandas behavior.
Some of the API to customize some aspects of pandas behavior are mentioned below:
The most important API functions are is They are −
  • get_option()
  • set_option()
  • reset_option()
  • describe_option()
  • option_context()
Let’s take an example to understand each one

get_option(param)

Pandas get_option() takes a single parameter and returns the value as shown below−

display.max_rows

This parameter when passed will display the default number of maximum row value. Here interpreter reads this value and displays the rows with this value as an upper limit to display.
import pandas as pd
print pd.get_option("display.max_rows")
Output
60

display.max_columns

When this parameter is passed it displays the default maximum number of values of the columns. Here, the interpreter reads this value and displays the columns with this value as an upper limit to display.
import pandas as pd
print pd.get_option("display.max_columns")
Output
20
Note: Here, 60 and 20 are the default configuration parameter values rows and columns respectively.

set_option(param,value)

Pandas has a set_option function which takes two arguments and sets the value to the parameter as given by the user.
Let’s take an example

display.max_rows

By using this set_option(), we can change the default number of rows to be displayed as per requirement.
Live Demo
import pandas as pd

pd.set_option("display.max_rows",80)

print pd.get_option("display.max_rows")
Output
100

display.max_columns

By using this set_option(), we can change the default number of columns to be displayed as per our requirement.
import pandas as pd

pd.set_option("display.max_columns",30)

print pd.get_option("display.max_columns")
Output
50

reset_option(param)

Pandas has another method reset_option which takes an argument and sets the value back to the default value.

display.max_rows

With this reset_option(), we can change the value back to the default number of rows to be displayed.
Let’s take an example
import pandas as pd

pd.reset_option("display.max_rows")
print pd.get_option("display.max_rows")
Output
60

describe_option(param)

describe_option helps us to print out the description of the argument.

display.max_rows

With reset_option(), we can change the value back to the default number of rows to be displayed.
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]

option_context()

This option_context is a context manager which is used to set the option in with a statement temporarily. Option values are restored automatically when you exit the with block −

display.max_rows

With option_context(), we can set the value temporarily.
Let’s take an example
import pandas as pd
with pd.option_context("display.max_rows",35):
   print(pd.get_option("display.max_rows"))
   print(pd.get_option("display.max_rows"))
Output
35
35
You may notice, 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 out the configured value as per our requirements.

Submit Review

We're Online!

Chat now for any query