Pandas Display Options. When the default setting does not meet... | by Soner Yıldırım | Towards Data Science

Pandas is a very powerful Python data analysis library that expedites the preprocessing steps of your project. The core data structure of Pandas is DataFrame which represents data in tabular form with labeled rows and columns. Pandas comes with many display options for a DataFrame. The default settings work well in most cases but you may need adjust them depending on the characteristics of the dataset. With a wide range of setting options, pandas allows to create a tailormade display preference.
Display options can be handled with two functions:
get_option: Shows the option for a setting
set_option: Allows to change the option
Let’s import a sample dataframe and go through with examples:
import pandas as pdimport numpy as npdf = pd.read_csv("/content/Telco-Customer-Churn.csv")df.shape(7043, 21)df.head()
The dataframe has 21 columns but only 10 columns are displayes. The ones in the middle are represented by dots. We can easily adjust it but let’s first learn the option for displaying columns:
pd.get_option("display.max_columns")10
It is 10, as expected. We can adjust it with a similar syntax using set_option function and indicating how many columns to display:
pd.set_option("display.max_columns", 25)df.head()
All of the columns are displayed now. They do not fit the screen but we can see them using the scroll bar at the bottom.
This option is useful when we have wide dataframes that contain many features of an observation.
A similar option exists for displaying rows:
pd.get_option("display.max_rows")60
So if we want to view 100 rows, we can adjust this setting similarly:
pd.set_option("display.max_rows", 100)
There is also an option to adjust the displayed width of a column. In some cases, datasets include long strings which are too long to display with default setting. If we want to view the complete string, we can use max_colwidth parameter. Let’s first see the default option:
pd.get_option("display.max_colwidth")50
So if a cell includes more than 50 characters, we are not able to see all of it. I created a simple dataframe to show how it looks with truncated view:
Let’s increase the width to see the complete text:
pd.set_option("display.max_colwidth", 150)
Another display option we may need to adjust is the precision of floating point numbers. The default value should work just fine but there might be some extreme cases that require more precision:
pd.get_option("display.precision")6
Default value is 6. Let’s increase it to 10. We may also want to decrease it to make it look simpler though.
pd.set_option("display.precision", 10)
There are many more Pandas display options that can be adjusted. I wanted to emphasize the ones that you may need more frequently. If you would like to see the whole list, you can always visit Pandas documentation.
If you would like to learn more about Pandas, here is a list of detailed Pandas posts:
A Complete Pandas Guide
Time Series Analysis with Pandas
Handling Missing Values with Pandas
Pandas — Save Memory with These Simple Tricks
Reshaping Pandas DataFrames
Pandas String Operations — Explained
Pandas Groupby — Explained
How to “read_csv” with Pandas
Thank you for reading. Please let me know if you have any feedback.