8 Commonly used Pandas display options you should know | by B. Chen | Towards Data Science

Pandas is one of the most important libraries for Data Manipulation and Analysis. It not only offers data structure & operations for manipulating data but also prints the result in a pretty tabular form with labeled rows and columns.
In most cases, the default settings of Pandas display should work well, but you may want your data to be displayed in some format that other than its default one. Pandas has an options system that allows you to customize display-related options. Display options can be configured using either methods or attributes as follows:
# Use methodsimport pandas as pdpd.set_option()pd.get_option()# Use attributes, for example display max_rowspd.option.display.max_rows
In this article, we’ll take a look at the 8 commonly used display options. This article is structured as follows:
Showing more rowsShowing more columnsSetting the max characters to be displayedSetting the precision for float columnsFormating the display for large numbersChanging plotting backendConfiguring the output of info()Resetting display options
Showing more rows
Showing more columns
Setting the max characters to be displayed
Setting the precision for float columns
Formating the display for large numbers
Changing plotting backend
Configuring the output of info()
Resetting display options
Please check out the Notebook for source code
For demonstration, we will be using the Titanic dataset available on Kaggle.
By default, Pandas doesn’t want to overrun your display and it will truncate the rows in the middle if the display.max_rows is exceeded. You can find the value by running the following statement:
pd.get_option('display.max_rows')# pd.options.display.max_rows60
Once the display.max_rows is exceeded, the display.min_rows (It defaults to 5) option determines how many rows are shown in the truncated representation. For instance, running df, it displays the first 5 rows, truncates all rows in the middle, and then displays the last 5 rows.
If you would like to change the display.max_row, for example, set it to 200 rows:
pd.set_option('display.max_rows', 200)# pd.options.display.max_rows = 200
You can also set the value to None to show all rows. But be careful with this as it’s not going to work very well if you have an extremely long table.
pd.set_option('display.max_rows', None)# pd.options.display.max_rows = None
And to reset the value:
# To resetpd.reset_option('display.max_rows')
Similarly, there is a display.max_columns option for columns and the value defaults to 20 :
pd.get_option('display.max_columns')# pd.options.display.max_columns20
If you would like to change the value, for example, set it to 30:
pd.set_option('display.max_columns', 30)# pd.options.display.max_columns = 30
Similarly, you can also set the value to None to show all columns. But be careful with this as it’s not going to work very well if you have an extremely wide table (With a lot of features).
pd.set_option('display.max_columns', None)# pd.options.display.max_columns = None
And to reset the value:
pd.reset_option('display.max_columns')
Running df.head() and you may find an ellipsis (...) showing in the 2nd row. That is because Pandas has some constraints on how many characters it will display and the default value is 50 characters.
pd.get_option('display.max_colwidth')# pd.options.display.max_colwidth50
If you would like to change the value, for example, set it to 500 characters:
pd.set_option('display.max_colwidth', 500)# pd.options.display.max_colwidth = 500
Similarly, you can also set the value to None to show all characters. But be careful with this as it’s not going to work very well if you have a column with extremely long text.
pd.set_option('display.max_colwidth', None)# pd.options.display.max_colwidth = None
And finally to reset the value:
pd.reset_option('display.max_colwidth')
You may notice that Pandas also has some constraints on the number of decimals to be displayed for the float column.
pd.get_option('display.precision')# pd.options.display.precision6
By default, Pandas will only display 6 digits after the decimal point if it has much more decimals.
If you would like to change the value, for example, set it to 2 decimals:
pd.set_option('display.precision', 2)# pd.options.display.precision = 2
Note this setting is not affecting the underlying data, it is only affecting the float column display.
And to reset the value:
pd.reset_option('display.precision')
It’s always better to format the display for numbers, for example, currency, decimal, percent, etc. Pandas has an option to format any float column using display.float_format option.
Note this is only working for the float column, for other data types, you will have to convert them into float. If you want to learn more about converting data into float, you can check out:
towardsdatascience.com
Big number, for example 1200000, are always challenge to read. It
pd.set_option('display.float_format',  '{:,}'.format)df_test
Suppose we only concern 2 digits after the decimal point, we can use this option to format the display as follows:
pd.set_option('display.float_format',  '{:,.2f}'.format)df_test
Suppose we are showing a percentage column, and we can use this option to format the display with a percent sign:
pd.set_option('display.float_format', '{:.2f}%'.format)df_test
And to format with a dollar sign
pd.set_option('display.float_format', '${:.2f}'.format)df_test
Pandas provides plotting functions on its DataFrame and Series. By default, matplotlib is used as the plotting backend. Since version 0.25, Pandas has provided an option to use a different backend.
To change it to a different backend than the default matplotlib, you first need to install the 3rd party libraries, for example:
Install plotly for Pandas Plotting Backend
Install altair for Pandas Plotting Backend
Then, just need to set plotting.backend to 'ploty' or 'altair' and call the plotting method just like you use those 3rd party libraries, for example:
import pandas as pdimport numpy as nppd.set_option('plotting.backend', 'altair')data = pd.Series(np.random.randn(100).cumsum())data.plot()
To reset to the default plotting backend:
pd.reset_option('plotting.backend')
Most of us are using Pandas info() method to quickly check the profile of the DataFrame. However, the method has a default constraint on the max number of columns to be profiled and can be very slow when counting null from a big dataset.
Pandas provides two options:
display.max_info_columns: defaults to 100 and it sets the max number of columns to be profiled.
display.max_info_rows: defaults to 1690785 and it sets the threshold for when counting null.
When profiling a dataset with 150 features, we can set display.max_info_columns to a value that covers all columns. For example, sets it to 200 columns:
pd.set_option('display.max_info_columns', 200)
When profiling a large dataset, df.info() can be very slow due to the task to count all null. We can simply set display.max_info_rows to a small value to avoid counting it, for example, only counting null when the number of rows does not exceed 5:
pd.set_option('display.max_info_rows', 5)
pd.describe_option() will print out the description of settings and their current value.
pd.describe_option()
You can also use it to print a specific option, for example, rows display options
# specific searchingpd.describe_option('rows')
Finally, you can reset all options by running the following statement:
pd.reset_option('all')
In this article, we have learned 8 commonly used Pandas Display options that can help you format and improve your Pandas output.
I hope this article will help you to save time in learning Data Analysis with Pandas. I recommend you to check out the documentation for Pandas Options & Setting and to know about other things you can do.
Thanks for reading. Please check out the notebook for the source code and stay tuned if you are interested in the practical aspect of machine learning.
10 tricks for converting numbers and strings to Datetime in Pandas
All Pandas json_normalize() you should know for flattening JSON
Using Pandas method chaining to improve code readability
How to do a Custom Sort on Pandas DataFrame
All the Pandas shift() you should know for data analysis
When to use Pandas transform() function
Pandas concat() tricks you should know
Difference between apply() and transform() in Pandas
All the Pandas merge() you should know
Working with datetime in Pandas DataFrame
Pandas read_csv() tricks you should know
4 tricks you should know to parse date columns with Pandas read_csv()
More tutorials can be found on my Github