Display Options of Pandas Dataframes

Display Options of Pandas Dataframes

‍In data analysis and data cleaning tasks, working with large datasets can sometimes be challenging. The default layout of Pandas dataframes may not always be the most suitable for efficient data manipulation and analysis. Fortunately, the display options of Pandas dataframes allows to customize the appearance and behavior of dataframes according to your specific needs.

Understanding the Pandas Options API

Pandas offers an API consisting of five essential functions for customizing its behavior. These functions are:

  1. get_option(): Retrieves the current value of a specified option.
  2. set_option(): Sets the value of a specified option.
  3. reset_option(): Resets the value of a specified option to its default value.
  4. describe_option(): Provides a description of a specified option.
  5. option_context(): Temporarily sets an option within a specific context.

How to change the Maximum Number of Rows to Display

To adjust the maximum number of rows to display in a dataframe, you can use the "display.max_rows" option. By default, Pandas displays 60 rows. To change this value:

import pandas as pd

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

In this example, we have set the maximum number of rows to display to 80. You can replace 80 with any desired value.

How to Change the Maximum Number of Columns

Similarly, you can modify the maximum number of columns to display in a dataframe using the "display.max_columns" option. By default, Pandas shows 20 columns. To change this value:

import pandas as pd

## We set the maximum number of columns to 30
pd.set_option("display.max_columns", 30)

How to Adjust the Width of Columns

To edit the width of columns in the Notebook, Pandas has the "display.max_colwidth" option. The width is set to 50 characters by default. To change this value:

pd.set_option("display.max_colwidth", 100)

In this example, we have set the maximum column width to 100 characters.
To show all the content of the columns in the table, enter “-1”. It is useful when dealing with comments, descriptions or reviews and you need to see the whole text.


pd.set_option("display.max_colwidth", -1)

How to Change the Precision of Decimal Numbers

When working with float numbers in a dataframe, you may want to control the precision of the displayed decimal places. Pandas displays 6 decimal places. To change this value:

## We set 4 decimals for numeric columns
pd.set_option("display.precision", 4)

How to remove the warnings of the Jupyter Notebook

Sometimes, when modifying the display options of a dataframe, you may encounter warnings related to deprecated behavior or versions updates under the notebook cell you are executing. To suppress these warnings, you can use the "warnings" library in Python. Simply import the library and use the "filterwarnings()" function to ignore specific warning messages:

import warnings
warnings.filterwarnings("ignore")
# Rest of your code here

Hoew to Retrieve Current Option Values

To retrieve the current value of a specific option, you can use the "get_option()" function. This function takes the option name as a parameter and returns its current value:

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

In this example, we are retrieving the current value of the display.max_rows option, which represents the maximum number of rows to display in a dataframe.

How to Reset Options to their Default Values

If you have modified the display options of a dataframe and want to revert them to their original values, you can use the "reset_option()" function. This function takes the option name as a parameter and resets it to its default value:

pd.reset_option("display.max_rows")

After executing this code, the maximum number of rows to display will be set back to 60.

Conclusion

Customizing the layout and appearance of Pandas dataframes can greatly enhance your data analysis and data cleaning workflow. Pandas offers the flexibility to tailor the appearance of your dataframes allowing to adjust various display options such as the number of rows and columns to show, the width of columns, the precision of decimal numbers, and more.

Python and Excel Projects for practice
Register New Account
Shopping cart