Try These Pandas Display Configurations in Your Next Analysis
Last Updated on June 26, 2022 by Editorial Team
Author(s): Hrishikesh Patel
Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.
Make your Jupyter notebook more presentable with these useful Pandas display customizations
While analyzing data using Pandas, you might have faced the following display-related issues:
- Unable to see the entire text if they are lengthy. In the following image, URLs get shortened.
2. Pandas by default show large floating-point numbers using scientific notation, e.g. 1,000,000.5 is shown as 1.000e+06
3. Inconsistent decimal place accuracy among float type columns. E.g, in the following figure, col_1 has one digit after decimal point whereas col_2 has three digits post decimal point. Though this will not affect your analysis, it might not look good while sharing your notebook withΒ others.
In this story, I am going to cover how to solve these issues using the following common Pandas display customizations.
List ofΒ contents
- Customize how many rows toΒ display
- Customize how many columns toΒ display
- Customize columnΒ width
- Make decimal place accuracy consistent among floatΒ columns
- Disable the scientific notation
- Bonus!
Note: These options only change how data will be displayed. It does not affect underlying data.
- Customize how many rows toΒ display
When you print a large data frame, pandas display the first 5 and last 5 rows by default as illustrated below.
However, we can change how many rows to display by setting a value for the display option display.max_rows. Letβs set it toΒ 4.
You can also reset the option using pd.reset_option("display.max_rows") to return to the default behavior.
2. Customize how many columns toΒ display
You can customize the number of columns to show while printing the data frame by setting display.max_columns.
Like previous, you can also reset this option using pd.reset_option("display.max_columns") to return to the default behavior.
3. Customize columnΒ width
In the following image, we cannot see the full text for the first two rows as their character length exceedsΒ 50.
However, after setting display.max_colwidth it to 70, we can see the entire text. You can choose a different number based on yourΒ data.
This option can also be reset by using pd.reset_option("display.max_colwidth")Β .
4. Make decimal place accuracy consistent among floatΒ columns
Currently, col_1 and col_2 have inconsistent decimal place accuracy as depictedΒ below.
By setting display.float_format to "{:.2f}".format we can make the format consistent. As shown in the image below, the option will only affect the float columns, not the integerΒ columns.
This option can be reset using pd.reset_option("display.float_format")
5. Disable the scientific notation
Pandas by default show large float values in the scientific notation.
By setting display.float_format to "{:,.2f}".formatΒ , we can add a separator for thousands and also set decimal place accuracy to two decimals.
You can also add a $ sign before the numbers to show currency by setting display.float_format to "$ {:,.2f}".format.
6. Bonus
How to find all such useful display options when you are working offline? The trick is to use pd.describe_option() and youβll get a list of all available options.
However, if you are looking for a specific option, you can type the option name as an argument in pd.describe_option()Β . For example, pd.describe_option("max_rows") will print the description of display.max_rows theΒ option.
Reference
Pandas Options andΒ Settings
Before youΒ go!
I hope you have enjoyed the story and found it useful. Follow me on Medium if youβd like more stories like this and subscribe to me to get my new stories directly into yourΒ inbox.
My other stories you mightΒ enjoyβ¦
- Regular Expression (RegEx) in PythonΒ : The Basics
- Regular Expressions (RegEx) in PythonΒ : Advanced Concepts
Try These Pandas Display Configurations in Your Next Analysis was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Join thousands of data leaders on the AI newsletter. Itβs free, we donβt spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI