Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Pretty Print an Entire Pandas Series or DataFrame?

  • Last Updated : 13 Jan, 2021

In this article, we are going to see how to Pretty Print entire pandas Series / Dataframe.

There are 2 ways to Pretty Print entire pandas Series / Dataframe:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • Use pd.set_options() method
  • Use pd.option_context() method

Method 1: Using pd.set_options() method



Sets the value of the specified option. There are various pretty print options are available for use with this method.

For example display.max_columns, display.max_colwidth, display.max_rows, display.colheader_justify, display.precision, etc.  Some pretty print options are discussed below:

  • display.max_columns: The maximum number of columns pandas should print. If None is provided as an argument all columns are printed.
  • display.max_rows: The maximum number of rows pandas should print. If None is provided as an argument all rows are printed.
  • display.colheader_justify: Controls the alignment of column headers
  • display.precision: Floating point output precision in terms of a number of places after the decimal, for regular formatting as well as scientific notation.
  • display.date_dayfirst: When True, prints and parses dates with the day first, e.g. 20/12/2020
  • display.date_yearfirst: When True, prints and parses dates with the year first, e.g. 2020/12/20
  • display.width: Width of the display in characters. If set to None, pandas will correctly auto-detect the width.

Below is the implementation:

Python3




import pandas as pd
  
# Create a dataframe
df = pd.DataFrame({
  'Product_id': ['ABC', 'DEF', 'GHI', 'JKL'
                 'MNO', 'PQR', 'STU', 'VWX'],
    
  'Stall_no': [37, 38, 9, 50, 7, 23, 33, 4],
  'Grade': [1, 0, 0, 2, 0, 1, 3, 0],
    
  'Category': ['Fashion', 'Education', 'Technology'
               'Fashion', 'Education', 'Technology'
               'Fashion', 'Education'],
    
  'Demand': [10, 12, 14, 15, 13, 20, 10, 15],
  'charges1': [376, 397, 250, 144, 211, 633, 263, 104],
  'charges2': [11, 12, 9, 13, 4, 6, 13, 15],
  'Max_Price': [4713, 10352, 7309, 20814, 9261
                6104, 5257, 5921],
    
  'Selling_price': [4185.9477, 9271.490256, 6785.701362
                    13028.91782, 906.553935, 5631.247872
                    3874.264992, 4820.943]})
display(df)

Output:

We will use some options of set_options() method on above df to see all rows, all columns,all columns in one row with center aligned column headers and rounding number of places after the decimal for each floating value to 2.

Python3






pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 1000)
pd.set_option('display.colheader_justify', 'center')
pd.set_option('display.precision', 2)
display(df)

Output:

Once set through pd.set_options() method, the same settings are used with all the next dataframe printing commands. 

Method 2: Using pd.option_context()

pd.set_option() method provides permanent setting for displaying dataframe. pd.option_context() temporarily sets the options in with statement context. pd.option_context() is a one liner code for above pd.set_options() and have temporary effect. 

Following code prints the above df with 5 rows, all columns, all columns in one row with left-aligned column headers and rounding number of places after the decimal for each floating value to 3.

Python3




with pd.option_context('display.max_rows', 5,
                       'display.max_columns', None,
                       'display.width', 1000,
                       'display.precision', 3,
                       'display.colheader_justify', 'left'):
    display(df)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!