Pandas Options – Explore 5 Core Options to Customize Your Data

Pandas are the basic library for data science, data analytics, and data processing. Pandas options allow a user to customize the data according. In this article, we are going to cover 5 different types of Pandas Options and Customizations with examples.

Pandas Options and Customizations

Pandas have some default factors which restrict the analysis of data. Therefore to have a stronghold over the library and to make the most out of its uses, it is important to know the various methods to change the default pandas values.

Common default values-

  • display.max_rows and display.max_columns which shows the default number of rows and columns.
  • display.max_colwidth which gives us the maximum width of the column
  • display.expand_frame_repr which gives us DataFrames that is spread across numerous pages.
  • display.precision gives us the precision of the decimal numbers

Do you know How to use function applications of Pandas?

Types of Pandas Options and Customization

Before we start Pandas options and customization, we need to import the pandas library:

>>>import pandas as pd

There are 5 types of Pandas options to customize the data –

Pandas Options and Customizations

1. Pandas.get_option()

With the help of .get_option in pandas, we can define parameter which will give us a particular detail about the default values in pandas.

Using “display.max_rows” and “display.max_columns” as parameters we get a maximum number of rows and columns that can display by default.

>>> dataflair= pd.get_option("display.max_rows")
>>> print(dataflair)

Output:

60

display max rows in get option

>>> dataflair2= pd.get_option("display.max_columns")
>>> print(dataflair2)

Output:

0

display max coloums in get option

2. Pandas.set_option()

The .set_option() function allows us to change a default value to something of our choice. In the example given below, we see that we can change the “display.max_rows” from 60 to 90.

>>> pd.set_option("display.max_rows",90)
>>> dataflair3= pd.get_option("display.max_rows")
>>> print(dataflair3)

Output:

90

Take the easy steps to sort pandas dataframes and series

>>> pd.set_option("display.max_columns",10)
>>> dataflair4= pd.get_option("display.max_columns")
>>> print(dataflair4)

Output:

10

Pandas Set Option Example

3. Pandas.reset_option

With the help of the .reset_option in Pandas, you can get back the default values which may change previously.

>>> pd.reset_option("display.max_rows")
>>> dataflair5= pd.get_option("display.max_rows")
>>> print(dataflair5)

Output:

60

>>> pd.reset_option("display.max_columns")
>>> dataflair6= pd.get_option("display.max_columns")
>>> print(dataflair6)

Output:

0

Pandas reset Option with Example

4. Pandas.describe_option

The .describe_option in Pandas describes the parameter. For example .describe_option(“display.max_rows”) would give the details about “display.max_rows” .

>>> pd.describe_option("display.max_rows")

Struggling with the installation of Pandas

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]

>>> pd.describe_option("display.max_columns")

Output:

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: 0] [currently: 0]

Example of Pandas Describe Option

5. Pandas.option_context

Using .opton_context function we can invoke a pandas option function which will be only active within the scope of the function.

In the below example, display.max_rows is set to 30 only inside the .option-context scope. Outside the function scope, it returns back to being 60.

>>> with pd.option_context("display.max_rows",30):
      print(pd.get_option("display.max_rows"))

Output:

30

Don’t forget to check out Pandas Basic Functionality

>>> print(pd.get_option("display.max_rows"))

Output:

60

Example of pandas Option Context

Summary

We have studied Pandas options and customization functions, now you are ready to use and customize pandas accordingly. This will allow you to have a better grasp over Pandas and derive the most out of the library.

For queries and suggestions, you can approach our comment section.

Excited for our next Pandas Tutorial? Check out how to create panels in Pandas?

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.