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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/scope("`Scope`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-65450{{"`Pandas Options and Settings`"}} python/with_statement -.-> lab-65450{{"`Pandas Options and Settings`"}} python/variables_data_types -.-> lab-65450{{"`Pandas Options and Settings`"}} python/for_loops -.-> lab-65450{{"`Pandas Options and Settings`"}} python/tuples -.-> lab-65450{{"`Pandas Options and Settings`"}} python/scope -.-> lab-65450{{"`Pandas Options and Settings`"}} python/importing_modules -.-> lab-65450{{"`Pandas Options and Settings`"}} python/standard_libraries -.-> lab-65450{{"`Pandas Options and Settings`"}} python/data_collections -.-> lab-65450{{"`Pandas Options and Settings`"}} python/numerical_computing -.-> lab-65450{{"`Pandas Options and Settings`"}} python/data_analysis -.-> lab-65450{{"`Pandas Options and Settings`"}} python/build_in_functions -.-> lab-65450{{"`Pandas Options and Settings`"}} end

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