Explain Codes LogoExplain Codes Logo

Pretty-print an entire Pandas Series / DataFrame

python
pandas
dataframe
display-settings
Anton ShumikhinbyAnton Shumikhin·Nov 1, 2024
TLDR

To beautify your Pandas output instantly, use the pd.set_option method. It helps adjust the display settings for a clear view of your DataFrame. Do it all in one block:

import pandas as pd # Introduce your DataFrame as 'df' pd.set_option('display.max_rows', None) # No row hides from you pd.set_option('display.max_columns', None) # Don't let columns play hide-n-seek pd.set_option('display.expand_frame_repr', False) # Bid goodbye to wrapping pd.set_option('display.precision', 2) # Round up those decimals print(df) # Voila! All data dancing before your eyes.

You have now declared open visibility for all your data, said NO to wrapping for wide DataFrames, and rounded off values for an easy-on-eye viewing.

Temporary Context Setting

Sometimes, you may want to change the display settings temporarily without affecting the global settings. Use pd.option_context for such cases:

with pd.option_context('display.max_rows', 10, 'display.max_columns', 5): # Only 10 rows & 5 columns are allowed to the party print(df)

This only shows 10 rows and 5 columns. Your studio-apartment version of DataFrame when throwing a smaller party.

Tabulate for Better Visualization

Take your DataFrame presentation a level-up with SQL-style tables. But first, check if tabulate is on the guest list:

pip install tabulate

If not, invite it to the party with the above command. Now, serve your DataFrame in style with tabulate:

from tabulate import tabulate print(tabulate(df, headers='keys', tablefmt='psql')) # Make way for the elite guest - psql formatted DataFrame

Your DataFrame now knows how to make an entrance!

Don't Let Truncation Steal the Show

If you're in a non-Jupyter environment like terminal or command-line interfaces, replace display(df) with print(df):

print(df.to_string()) # Unleash the complete DataFrame, column by column, row by row!

Now, your complete DataFrame is printed as it is - without being cut short.

Exploit Text Field Width

Don't lose vital information from lengthy text fields by inadvertent truncation. Instead, set:

pd.set_option('display.max_colwidth', None)

Every text field now tells a complete story, literally!

Dealing with Large DataFrames

Handling large DataFrames could be resource intensive. Here, efficient use prevents waste. Enter print_full:

def print_full(x): pd.set_option('display.max_rows', None) print(x) pd.reset_option('display.max_rows') # Resize rows to fit into their skinny jeans again!

This function lets all rows make an appearance, then reverts to usual, thereby avoiding resource overkill.

Remembering Preferences with IPython

For custom tweaks in Jupyter environment that are persistent, tweak the IPython configuration:

c.InteractiveShellApp.exec_lines = [ "pd.set_option('display.precision', 3)" # 'cause everyone likes some shape to those values! ]

Your custom settings now stick around for every session!