Pandas Options and Settings

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab focuses on understanding how to configure and customize global behavior related to Pandas DataFrame display, data behavior, and more. We will explore how to get/set options, reset options to their default values, and describe options. We will also learn how to execute a code block with a set of options that revert to prior settings after execution.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing Pandas

Let's start by importing the Pandas library. This is a powerful data manipulation library in Python.

## Importing pandas library
import pandas as pd

Getting and Setting Options

We can get or set the value of a single option using pd.get_option or pd.set_option respectively. Here, we are setting the maximum display rows to 999.

## Get the current setting for maximum display rows
print(pd.options.display.max_rows)

## Set the maximum display rows to 999
pd.options.display.max_rows = 999

## Verify the new setting
print(pd.options.display.max_rows)

Resetting Options

If we wish to reset one or more options to their default value, we can use pd.reset_option.

## Reset the maximum display rows to default
pd.reset_option("display.max_rows")

## Verify the reset
print(pd.options.display.max_rows)

Describing Options

To print the descriptions of one or more options, use pd.describe_option.

## Describe the 'display.max_rows' option
pd.describe_option("display.max_rows")

Using option_context

The option_context function allows us to execute a code block with a set of options that revert to prior settings after execution.

## Execute a code block with a set of options
with pd.option_context("display.max_rows", 10):
    ## This will print 10 despite the global setting being different
    print(pd.get_option("display.max_rows"))

## This will print the global setting as the context block has ended
print(pd.get_option("display.max_rows"))

Setting Startup Options

We can create a startup script in the Python/IPython environment to import pandas and set options, which makes working with pandas more efficient.

## This is an example of a startup script
## Place this in a .py file in the startup directory of IPython profile
import pandas as pd

pd.set_option("display.max_rows", 999)
pd.set_option("display.precision", 5)

Summary

This lab guide explained how to get, set, and reset options in pandas. We also discussed how to describe options and use the option_context function. Finally, we explored how to set startup options in the Python/IPython environment. These techniques allow us to customize and configure the behavior of pandas to suit our needs.

Other Python Tutorials you may like