The world’s leading publication for data science, AI, and ML professionals.

6 Pandas Display Options You Should Memories

Don't waste your time on Googling every time!

Photo by congerdesign on Pixabay
Photo by congerdesign on Pixabay

As a Data Scientist, a Data Analyst or a Data Engineer, Pandas must be one of the most commonly used libraries in Python. It can print the Data Frame in a pretty HTML styled format for us, which is one of its major features if you’re using Jupyter Notebook/Lab or Google Colab like me.

Because Pandas use pre-defined HTML + CSS, we don’t need to worry about the format ourselves. However, sometimes we may want it to display in some format that other than its default one. You probably know that we can set pd.options.display to achieve this.

I have some many learners who know this but don’t remember the options every time they want to use. Well, there are 30+ different options in the pd.options.display sub-category only. So, it is definitely not necessary to remember them all.

In this article, I’ll suggest 6 of them that I recommend you should memorise to improve the coding efficiency. Don’t waste your time on Googling/Stack Overflow every time you want to use them!

Please note that I’ll use pd as the alias of Pandas as I believe most people are using import pandas as pd like I do.


1. Set Max Number of Rows

pd.options.display.max_rows

When we have a data frame with more rows, Pandas will truncate the rows in the middle to make sure that we won’t be bothered with an extremely long table. The default number is 60.

As shown, if we have a data frame with more than 60 rows, 50 rows in the middle will be truncated.

pd.DataFrame(np.random.randn(61, 5))

If we set the option larger than the number of rows of our data frame, all the rows will be displayed. For example, let’s set it to 100 rows.

pd.options.display.max_rows = 100

2. Set Max Number of Columns

pd.options.display.max_columns

Of course, the truncate logic applies to the horizontal direction, too. The default number of the max number of columns is 20.

If we don’t change it and having a data frame with more than 20 columns, we won’t be able to view the columns in the middle again.

pd.DataFrame(np.random.randn(5, 21))

If we want to view all the columns, just make the max number of columns larger.

pd.options.display.max_columns = 50

3. Set Max Width of Cell

pd.options.display.max_colwidth

Not only the number of rows and columns, but the width of every cell also has constraints on its width. By default, Pandas only display content in a cell with a maximum width of 50. That is, a cell with more than 50 characters will be truncated.

pd.DataFrame({
    'col': [''.join(list("Towards Data Science.") * 3)]
})

In the above code, I just repeated the string "Towards Data Science." 3 times which has 63 characters in total. So, the tail is truncated.

If we set the max column width to 80, all the text will be displayed.


4. Set Max Number of Columns Shown in info()

pd.options.display.max_info_columns

I believe most of us will use df.info() to quickly check the profile of the data frame. However, sometimes it won’t show all the columns in the results. This is because the info() method has a default constraint on the max number of columns to be profiled, which is 100 columns.

Let’s create a random data frame with more than 100 columns and use info() method to get its profile.

df = pd.DataFrame(np.random.randn(5, 101))
df.info()

This is not expected. I want to see the "Dtype" and the "Non-Null Count" for all the columns. In this case, the max info columns option can help.

pd.options.display.max_info_columns = 150
df.info()

5. Set Display Precision

pd.options.display.precision

You may or may not noticed that Pandas actually has a built-in constraint on the number of decimals to be displayed in a data frame. Let’s use the data frame df that is generated in the previous demo as an example.

Pay attention to the first cell, which has a float number -0.939665. This is because Pandas will only display 6 digits after the decimal point. If we get the number out of the data frame as follows, we can then see that it has much more decimals -0.9396645167554308.

df.iloc[0, 0]

Don’t worry. This won’t affect the actual numbers that are going to be used in your algorithm later on, because it is just for display. However, what if I want to see the numbers with more precisions? Let’s set the option to 15.

pd.options.display.precision = 15

Now, it can display the whole number.


6. Set Decimal Format

pd.options.display.float_format

Sometimes we may want to show our job to someone else, or we may want to view the data frame prettier by ourselves. It is important because I believe insights comes from a neat presentation. A messy presentation will waste the resource in our brain.

For example, let’s consider the numbers in the data frame we used in the above examples should be percentages, and we only concern 2 digits after the decimal point. Then, we can use this option to set the displaying format as follows.

pd.options.display.float_format = '{:.2f}%'.format

As shown, the format is flexible enough for you to make it whatever suits your needs, such as {$(:,.2f)} to display currency.

Summary

Photo by Shutterbug75 on Pixabay
Photo by Shutterbug75 on Pixabay

In this article, I have introduced 6 Pandas display options that we would better to memorise. There are more than 30 of them, but these ones are what I found the most commonly used ones. In order to prevent wasting time on Googling every time, it is highly recommended to memorise them.

Join Medium with my referral link – Christopher Tao

If you feel my articles are helpful, please consider joining Medium Membership to support me and thousands of other writers! (Click the link above)


Related Articles