Explain Codes LogoExplain Codes Logo

How can I display full (non-truncated) dataframe information in HTML when converting from Pandas dataframe to HTML?

python
dataframe
display-settings
pandas
Anton ShumikhinbyAnton Shumikhin·Sep 5, 2024
TLDR

To prevent Pandas dataframes from truncating in HTML, tweak your pd.set_option parameters. Settings such as 'display.max_columns' and 'display.max_rows' should be set to None prior to using the to_html() method.

import pandas as pd # Adjust display settings like a pro pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) # Enter the mighty dataframe df = pd.DataFrame({'A': range(50), 'B': range(50, 100)}) # Cast a spell to print the full dataframe as HTML print(df.to_html())

Also, to ensure complete cell content visibility, especially for that chatty cell content:

pd.set_option('display.max_colwidth', None) # For Pandas 1.0 and later # or if you're using a time machine and you're back to old Pandas versions : pd.set_option('display.max_colwidth', -1) # For Pandas versions before 1.0

Unleashing display settings

Here's some additional mojo for when you're stuck with wide dataframes or in need to demonstrate the precision of your float format:

  • Prevent horizontal wrapping and make sure your columns line up for their group photo:
pd.set_option('display.expand_frame_repr', False)
  • For widescreen-view lovers, get rid of the boundaries:
pd.set_option('display.width', 2000) # No boundaries, no worries.
  • If you're dealing with floats and you wish to customize their format. Flaunt them decimals!
pd.set_option('display.float_format', '{:.2f}'.format) # Because ".2f" really matters!

Temporarily printing detailed views

For that occasional moment when you need to display a huge DataFrame entirely:

def print_full(df): with pd.option_context('display.max_rows', None, 'display.max_colwidth', None): print(df) print_full(df) # Show me all your secrets, dataframe!

Rest assured, these changes only last for a hot second, keeping the rest of your code untouched and working just fine.

Jupyter notebook rendering

Working within Jupyter notebooks, you say? You're in the right place. Here's what you'll need:

from IPython.display import display display(df) # Voila! A whole dataframe!

Making your content work for you

Cut to the chase! Concentrate on important stuff in your code and explanations. Avoid huff and puff:

  • Condense info where possible.
  • Cut the fluff! Concentrate on key points.
  • Remember, you are a coder, not a poet! Save space

Disclosing HTML power-ups

Remember, you can jazz up your user's experience with interactive tables:

  • Bring in DataTables or other JavaScript libraries for sorting, searching, and pagination fun.
  • Use CSS for better-looking, easy-to-read tables.

Think about your audience's needs:

  • Non-technical audience? Simplify and keep it clean.
  • Developers or analysts? Bring out the detail and let it shine.