• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:17 mins read
You are currently viewing Pretty Print Pandas DataFrame or Series?

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 lots 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.

Advertisements

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.


# 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.


# 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

Alternatively, we can also use options.display to reduce the row and column size of a Pandas DataFrame which ideally prints the DataFrame pretty in the console. The 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.


# 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)

Frequently Asked Questions on Pretty Print Pandas DataFrame or Series

What does “pretty printing” mean in the context of Pandas DataFrame or Series?

Pretty printing refers to formatting data in a visually appealing and readable manner. In Pandas, it involves displaying DataFrame or Series data with appropriate spacing, alignment, and without truncation.

Why is pretty printing important for DataFrame or Series?

Pretty printing enhances data readability, making it easier for analysts, data scientists, and stakeholders to interpret and understand the data quickly. It ensures that data is presented in a clear and organized manner.

How can I pretty print a Pandas DataFrame or Series?

You can use the to_string() method with various formatting options to pretty print a DataFrame or Series. Additionally, you can use pd.option_context() to temporarily set display options for more customization.

What are some common formatting options for pretty printing DataFrame or Series?

Common formatting options include controlling the maximum number of rows and columns displayed (display.max_rows, display.max_columns), setting the precision of floating-point numbers (float_format), and controlling the width of the display (display.width).

How can I prevent DataFrame or Series from truncating when pretty printing?

You can set display.max_rows and display.max_columns to None to display all rows and columns without truncation. Additionally, you can adjust the display width (display.width) to accommodate wider data.

Does pretty printing affect the underlying data in DataFrame or Series?

Pretty printing only affects the visual representation of data when displaying it. It does not modify the underlying data in the DataFrame or Series.

Conclusion

In this article, I have explained how to pretty print an entire Pandas DataFrame to a 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 !!