Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Try These Pandas Display Configurations in Your Next Analysis
Latest

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

Image byΒ author

While analyzing data using Pandas, you might have faced the following display-related issues:

  1. Unable to see the entire text if they are lengthy. In the following image, URLs get shortened.
Lengthy text gets truncated in Pandas (image byΒ author)

2. Pandas by default show large floating-point numbers using scientific notation, e.g. 1,000,000.5 is shown as 1.000e+06

Large floats are shown using scientific notation (image byΒ author)

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.

Inconsistent precision among columns (image byΒ author)

In this story, I am going to cover how to solve these issues using the following common Pandas display customizations.

List ofΒ contents

  1. Customize how many rows toΒ display
  2. Customize how many columns toΒ display
  3. Customize columnΒ width
  4. Make decimal place accuracy consistent among floatΒ columns
  5. Disable the scientific notation
  6. Bonus!

Note: These options only change how data will be displayed. It does not affect underlying data.

  1. 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.

Pandas by default display 10 rows (image byΒ author)

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.

Displays 4 rows after setting β€˜display.max_rows’ to 4 (image byΒ author)

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.

Displays 6 columns by setting `display.max_columns’ to 6 (image byΒ author)

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.

Lengthy text gets shortened in Pandas (image byΒ author)

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.

Displays full text by setting β€˜display.max_colwidth’ toΒ 70

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.

Inconsistent decimal accuracy among float columns (image byΒ author)

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.

Making decimal place accuracy consistent among float columns by setting β€˜display.float_format’ to β€œ{:.2f}”.format (image byΒ author)

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.

Large floating-point numbers are displayed in scientific notation (image byΒ author)

By setting display.float_format to "{:,.2f}".formatΒ , we can add a separator for thousands and also set decimal place accuracy to two decimals.

Add thousands separator by setting β€œdisplay.float_format” to β€œ{:,.2f}”.format (image byΒ author)

You can also add a $ sign before the numbers to show currency by setting display.float_format to "$ {:,.2f}".format.

Add $ before a number by setting β€œdisplay.float_format” to β€œ$ {:,.2f}”.format (image byΒ author)

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.

Get the description of β€œdisplay.max_rows” option using β€˜pd.describe_option(β€œmax_rows”)’ (image byΒ author)

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…


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

Feedback ↓