5 Tips to Customize the Display of Your Pandas Data Frame | by Satyam Kumar | Towards Data Science

Pandas is one of the most popular Python libraries in the data science community, as it offers flexible data structures and a vast API for data explorations and visualization. A data scientist spends most of the time exploring the data and performing exploratory data analysis. Jupyter Notebook provides an interactive platform to perform exploratory data analysis and is most preferred by Data Scientists and Data Analysts.
dataframe.head() is a function from the Pandas package to display the top 5 rows of the data frame. Pandas use predefined HTML+CSS commands to display the data frame in a formatted way on the notebook. By default, we are limited by the number of rows and columns that the Pandas data frame shows in the notebook. Sometimes, it's necessary to change the display format to make the EDA more intuitive and formatted. The formatting of the data frame can be changed using pandas.options.display option.
30+ display options can be altered to customize the display format; in this article we will discuss 5 of the popular tricks to customize the display format of the data frame to make it more intuitive.
Checklist:1. Display of Columns2. Display of Rows3. Width of the cell4. Decimal value formatting5. Data frame info
Before getting started, I am using pd as alias name of pandas library, import pandas as pd , and df is the variable that points to the dataset.
After we load the dataset using Pandas, we are limited by the number of columns in the output display cell. df.head() function from Pandas library is essential to get a first cut overview of the top 5 rows of the data frame. The number of the columns is limited, which makes it difficult to visualize the entire set of columns.
By default, the output display is restricted to 20 columns, the first 10 and the last 10 columns are displayed separated by three dots.
You have the option to alter the number of columns to be displayed on the output cell. Using pd.set_option(“display.max_columns”, x), the number of displayed columns will be changed to x. If None is passed in place of x, then all the columns will be displayed in the output cell.
By changing the display options of the number of columns, it makes very intuitive to get the first cut visualization of the data frame.
Pandas also provide to alter the display options of the rows of the data frame. By default, Pandas will truncate the rows with more than 60 rows, and display only the top and bottom 5 rows in the output cell.
One can alter the number of rows to be displayed in the output cell using pd.set_option(“display.max_rows”, x) , when None replaced with x displays all the rows in the data frame. One can scroll down and visualize the values of the data frame.
Pandas display options can not only alter the number of rows and columns to be displayed in the output cell, but also provides capabilities to alter the width of the cell of the data frame. By default, the maximum width of the cell can be 50. The cell having more than 50 characters, the rest of the characters are truncated.
In the above sample dataset, the text values in the review column in truncated after 50 characters.pandas.display.options provides capabilities to alter the number of characters to be displayed in the output cell, using pd.set_option(“display.max_colwidth”, x) where x is the number of characters.
By default, Pandas formats the floating values in the data frame to 6 decimal places, for the values having more than 6 digits after the decimal.
If you print the first row values of V1 column, we get the real feature values that have 13 digits after the decimal-1.3598071336738 .
pandas.display.options provides capabilities to alter the precision of the floating-point numbers, using pd.set_option(“display.precision”, x) where x is the precision.
Changing the precision of the feature values does not actually affect the dataset, but it's restricted to the display in the output cell of the notebook.
dataframe.info() is a popular function in Pandas, to get an overview profile of the data frame. This displays the column name, Non-null values count, the datatype of the column for the data frame.
The info() function has its constraints limited to a data frame with 100 features or columns. The below-mentioned image displays what info() function returns for a dataset with just 2 features.
For a dataset with more than 100 features, info() functions do not return with the column name, Non-null values count, the datatype for each of the columns.
One can alter the default constraints of max_info_columns, to get the entire profile overview of the data frame.
In this article, we have discussed how to alter the display options in the Pandas data frame. The 6 hacks mentioned in the article are very useful while performing data understanding and data explorations tasks. By altering the display options discussed above one can get a better visualization of the data frame in the notebook.
[1] Pandas Documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html
Thank You for Reading