How to Pretty-Print Pandas DataFrames and Series | Towards Data Science

When we have to work with large pandas DataFrames that may also have multiple columns and rows it’s important to be able to display the DataFrames in a readable format. This is probably useful when debugging your code, too.
By default, only a subset of columns is displayed to the standard output when a DataFrame is printed out and has fairly large number of columns. The displayed columns may even be printed out in multiple lines.
In today’s article we are going to explore how to configure the required pandas options that will allow us to “pretty-print” pandas DataFrames.
Suppose we have the following DataFrame:
Now if the number of columns exceeds the value of the display option display.max_rows then the output DataFrame may be incomplete like the one shown below. Only a subset of the columns is displayed (column4 and column5 are missing) while the remaining columns are printed in a multi-line fashion.
While the output is still somewhat readable it’s definitely not ideal to have columns left out or printed in multiple lines.
If your monitor is wide enough and is able to fit more columns you may probably have to tweak a few display options. The values I’ll use below may not work for your setup so make sure to adjust them accordingly. Personally, I use an ultra-wide monitor I am able to print out fairly high number of columns when necessary.
Now in order to display all the columns (if your monitor can fit them) and in just one line all you have to do is set the display option expand_frame_repr to False:
pd.set_option('expand_frame_repr', False)
display.expand_frame_repr
default: True
Whether to print out the full DataFrame repr for wide DataFrames across multiple lines, max_columns is still respected, but the output will wrap-around across multiple “pages” if its width exceeds display.width
Alternatively, instead of setting expand_frame_repr to False you can change the value of display.max_rows:
pd.set_option(‘display.max_rows’, False)
Note that if the columns are still printed in multiple pages, then you may have to adjust display.width as well.
Now if your DataFrame contains more than a certain number of rows, only a few records (coming from the head and tail of your df) will be displayed:
If you want to display a wider range (or even all) of rows you need to set display.max_rows to the number of rows you want to output. If you want to display all the rows set it to None:
pd.set_option('display.max_rows', None)
A better approach is to use option_context() which is a context manager that you can use to temporarily set specific options in the with statement context.
The are more display options you can tweak and change the way pandas DataFrames are displayed.
display.max_colwidth: This is the maximum number of characters are displayed for column names. If a certain column name overflows, a placeholder (...)will be added instead.
pd.set_option('display.max_colwidth', None)
display.precision: This is the precision that will be used for floating points. It specifies the number of places after the decimal.
display.width: This is the overall number of characters of the display. If you want to display more columns you may some times have to also adjust the display.width as well.
You can find the full list of display using describe_option():
pd.describe_option(‘display’) . 
If you are working with Jupyter Notebooks, instead of print(df) simply use display(df) and the width will be adjusted accordingly.
In todays article we discussed a few display options for pandas let you pretty-print DataFrames according to what you want to display and probably to what monitor you use, too.
Pandas comes with an options system that lets users adjust and customize the display functionality. We covered only a small subset of the available display options. Make sure to read the Options and Settings section of the official documentation.