Saturday, 17 October 2020

Python Pandas : Display full Dataframe (Print all rows & columns without truncation)

 

Display full contents of a dataframe

Pandas provides an operation system to customize the behavior & display related stuff. Using this options module we can configure the display to show the complete dataframe instead of truncated one. A function set_option() is provided in pandas to set these kind of options,

pandas.set_option(pat, value)

It sets the value of the specified option. Let’s use this to display full contents of a dataframe.

So, to display complete contents of a dataframe without any kind of truncation, we need to set these 4 options,

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', -1)

Setting to display All rows of Dataframe

In pandas when we print a dataframe, it displays at max_rows number of rows. If we have more rows, then it truncates the rows.

pandas.options.display.max_rows

This option represents the maximum number of rows that pandas will display while printing a dataframe.
If set to ‘None‘ then it means unlimited i.e. pandas will display all the rows in dataframe.

Setting to display All Columns in Dataframe

To print all the columns we need to set following option to None i.e.

display.max_columns

By setting this to None, we instruct pandas that it should not truncate columns and display all of them.
# Set it to None to display all columns in the dataframe
pd.set_option('display.max_columns', None)

Setting to display Dataframe with full width i.e. all columns in a line

To fit all the columns in same line we need to maximize the terminal width. That can be done by using following option,

display.width

If set to None and pandas will correctly auto-detect the width of dataframe and will display all columns in single line. 
# Width of the display in characters. If set to None and pandas will correctly auto-detect the width.
pd.set_option('display.width', None)

Setting to display Dataframe by maximizing column width

We can use the option,

display.max_colwidth

It is set to maximize the width in characters of a column in the dataframe while printing. When set to None, pandas will auto detect the max size of column and print contents of that column without truncated the contents.
# The maximum width in characters of a column in the repr of a pandas data structure
pd.set_option('display.max_colwidth', -1)

No comments:

Post a Comment