Pretty Print Pandas DataFrame or Series?

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:October 25, 2022

How to display or pretty print the entire pandas DataFrame/Series with out truncation rows, columns or column text? If you have larger DataFrame or Series with lot’s or columns and text in a column/cell is too big, by default pandas truncates the rows, columns and text at certain length. For example, by default width of column is truncated at 50 characters.

In order to Pretty Print the entire Pandas Dataframe or Series you need to set some options by using option_context, set_option, and options.display methods.

Some Important terms to use in pretty print options are discussed below.

  • display.max_columns: It defines the total number of columns pandas should print. If None is passed as an argument, all columns are printed.
  • display.max_rows: It defines the total number of rows pandas should print. If None is passed as an argument, all rows are printed.
  • display.width: It is also an important option that defines the width of the display in characters. If set to None, pandas will correctly auto-detect the width.
  • display.colheader_justify: Controls the alignment of column headers.
  • display.precision: Floating point output precision in terms of the number of places after the decimal, for regular formatting as well as scientific notation.

1. Quick Examples of Pretty Print Pandas Series/DataFrame

If you are in a hurry below are some quick examples of how to pretty print an entire pandas Series/DataFrame.


# Below are quick examples.

# Example 1: Display entire data frame
print(df.to_string())

# Example 1: use pd.option_context() 
# to pretty-print pandas dataframe 
with pd.option_context('display.max_rows', 4,
                       'display.max_columns', None,
                       'display.width', 1000,
                       'display.precision', 3,
                       'display.colheader_justify', 'left'):

# Example 2: use pd.set_options() 
# to reduce the size of a pandas dataframe  
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', 3)
pd.set_option('display.width', 1000)
pd.set_option('display.colheader_justify', 'center')
pd.set_option('display.precision', 2)

# Example 3: use options.display 
# to reduce the size of a pandas dataframe
def set_pandas_display_options() -> None:
    display = pd.options.display
    display.max_columns = 4
    display.max_rows = 3
    display.max_colwidth = 185
    display.width = None
set_pandas_display_options()

Python pandas is widely used for data science/data analysis and machine learning applications. It is built on top of another popular package named Numpy, which provides scientific computing in Python. pandas DataFrame is a 2-dimensional labeled data structure with rows and columns (columns of potentially different types like integers, strings, float, None, Python objects e.t.c). You can think of it as an excel spreadsheet or SQL table.

Let’s create a Pandas create DataFrame from Python dictionary in which keys are 'Courses', 'Fee', 'Duration‘, 'Discount‘, and values are taken as a list of corresponding key values.


import pandas as pd
technologies = {
  'id' :[101,102,103],
  "Proverb":["Be the change that you wish to see in the world",
      "Everyone thinks of changing the world, but no one thinks of changing himself.",
      "The purpose of our lives is to be happy."]
   }
df = pd.DataFrame(technologies)
print(df)

Yields below output.

pandas pretty print dataframe

1. Display Pretty Print Entire Pandas DataFrame Contents

Note that our DataFrame above displays pretty much all rows, columns but the text in row 2 got truncated at 50 characters. If you have too big dataframe and print is not displaying entire DataFrame, use print after converting DataFrame to String.


# Display entire DataFrame
print(df.to_string())

This is the easiest way to display the entire DataFrame to console or terminal with out truncating. By running above code, you will get the below output.

display pretty pandas dataframe

2. Use pd.option_context() to Pretty-Print Pandas DataFrame

In case if you can’t use the above method, you need to change the setting of pandas by using option_context() and in with statement context. Following code prints the DataFrame with four rows, all columns in a row with left-aligned column headers, and rounding the number of places after the decimal for each floating value.


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Hadoop","Python","PySpark","Spark","Spark","PySpark","Hadoop"],
    'Fee' :[20000,25000,26000,22000,24000,35000,20000,25000,26000],
    'Duration':['30days','40days','35days','40days','60days','45days','30days','40days','35days'],
    'Discount':[1000,2300,1200,2500,2000,2000,1000,2300,1200]
              }
df = pd.DataFrame(technologies)
print(df)

# use pd.option_context() to pretty-print pandas dataframe 
with pd.option_context('display.max_rows', 4,
                       'display.max_columns', None,
                       'display.width', 1000,
                       'display.precision', 3,
                       'display.colheader_justify', 'left'):
print(df)

Yields below output.


   Courses   Fee   Duration  Discount
0     Spark  20000  30days   1000    
1   PySpark  25000  40days   2300    
..      ...    ...      ...       ...
7   PySpark  25000  40days   2300    
8    Hadoop  26000  35days   1200    

[9 rows x 4 columns]

4. Use pd.set_options() to Display Few Columns of DataFrame

Let’s use the same above example but by using pandas.set_options() function, Depending on option you use, you need to call this function to change the settings. Let’s use this option to see all rows, three columns, all columns in a row with center-aligned column headers, and rounding the number of places after the decimal for each floating value to two.


# use pd.set_options() to reduce the size of a pandas dataframe  
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', 3)
pd.set_option('display.width', 1000)
pd.set_option('display.colheader_justify', 'center')
pd.set_option('display.precision', 2)
print(df)

Yields below output.


 Courses   ...  Discount
0  Spark    ...  1000    
1  PySpark  ...  2300    
2  Hadoop   ...  1200    
3  Python   ...  2500    
4  PySpark  ...  2000    
5  Spark    ...  2000    
6  Spark    ...  1000    
7  PySpark  ...  2300    
8  Hadoop   ...  1200    

[9 rows x 4 columns]

5. Use options.display to Reduce the Size of a Pandas DataFrame

Alternative, we can also use options.display to reduce the row and columns size of a Pandas DataFrame which ideally print the DataFrame pretty in console. Following code prints the DataFrame with two rows, four columns, all columns in a row with left-aligned column headers, and not rounding the number of places after the decimal for each floating value.


# use options.display to reduce the size of a pandas dataframe
def set_pandas_display_options() -> None:
    display = pd.options.display
    display.max_columns = 4
    display.max_rows = 3
    display.max_colwidth = 185
    display.width = None
set_pandas_display_options()
print(df)

Yields below output.


   Courses   Fee  Duration  Discount
0    Spark  20000  30days     1000  
..     ...    ...      ...       ...
8   Hadoop  26000  35days     1200  

[9 rows x 4 columns]

6. Complete Example of Pretty Print an Entire Series/DataFrame


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Hadoop","Python","PySpark","Spark","Spark","PySpark","Hadoop"],
    'Fee' :[20000,25000,26000,22000,24000,35000,20000,25000,26000],
    'Duration':['30days','40days','35days','40days','60days','45days','30days','40days','35days'],
    'Discount':[1000,2300,1200,2500,2000,2000,1000,2300,1200]
              }
df = pd.DataFrame(technologies)
print(df)

# use pd.option_context() to pretty-print pandas dataframe 
with pd.option_context('display.max_rows', 4,
                       'display.max_columns', None,
                       'display.width', 1000,
                       'display.precision', 3,
                       'display.colheader_justify', 'left'):
    print(df)

# use pd.set_options() to reduce the size of a pandas dataframe  
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', 3)
pd.set_option('display.width', 1000)
pd.set_option('display.colheader_justify', 'center')
pd.set_option('display.precision', 2)
print(df)

# use options.display to reduce the size of a pandas dataframe
def set_pandas_display_options() -> None:
    display = pd.options.display
    display.max_columns = 4
    display.max_rows = 3
    display.max_colwidth = 185
    display.width = None
set_pandas_display_options()
print(df)

7. Conclusion

In this article, I have explained how to pretty print an entire Pandas DataFrame to console or terminal by using option_context, set_option, and options.display functions with examples. You can use the same example to pretty print the Series.

Happy Learning !!

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Pretty Print Pandas DataFrame or Series?