Pretty-print an entire Pandas Series / DataFrame
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:
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:
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:
If not, invite it to the party with the above command. Now, serve your DataFrame in style with tabulate:
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)
:
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:
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
:
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:
Your custom settings now stick around for every session!
Was this article helpful?