Explain Codes LogoExplain Codes Logo

Pretty Printing a pandas DataFrame

python
dataframe
tabulate
markdown
Nikita BarsukovbyNikita Barsukov·Mar 2, 2025
TLDR

For a quick print of a pandas DataFrame, configure pd.options.display to optimize output. Set your max_columns and max_rows to None to dodge truncation, and apply df.to_string() to print it:

# Shh... pandas stealth mode! import pandas as pd # The birth of a DataFrame df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) # To truncate or not to truncate? That is the question. pd.options.display.max_columns = None pd.options.display.max_rows = None # Abracadabra! Print the full DataFrame print(df.to_string(index=False))

This makes sure your DataFrame is visually appealing and in a table layout that's easy on your eyes!

Take it to the next level

Tabulate: Your data's best friend

To print DataFrames as text-based tables, the tabulate library is where it's at. Remember to use headers='keys' and tablefmt='psql':

from tabulate import tabulate # "Pretty" doesn't even begin to cover it! print(tabulate(df, headers='keys', tablefmt='psql'))

This transforms your DataFrame into some pretty slick looking tables, ready for any report or presentation.

Markdown: Becoming a GitHub star

When going for GitHub, opt for to_markdown(). Yes, you can print your DataFrame as a markdown table:

# Because pandas knows what's trendy print(df.to_markdown())

HTML Tables: For browsers & notebooks

When dealing with webpages or Jupyter Notebooks, DataFrame.to_html() gives tables in HTML format. For an interactive display use IPython.display:

from IPython.display import display, HTML # Python just casually doing magic stuff! display(HTML(df.to_html()))

PrettyTable: ASCII never looked better!

Use prettytable library for a DataFrame converted to an in-memory CSV and printed as an ASCII table:

from prettytable import PrettyTable import io # Creating a memory door for the DataFrame...Walk through it! output = io.StringIO() df.to_csv(output) output.seek(0) pt = PrettyTable() pt.field_names = df.columns.tolist() for row in csv.reader(output): pt.add_row(row) # Just a DataFrame breaking ASCII! print(pt)

Beyond the basics

Unearth the 20+ table formats found in DataFrame.to_markdown(). Tailor these libraries to present your DataFrame in a style that mirrors your true self!

Further enhancements

Interactive displays: For the web lords

Interactive table displays become imperative when dealing with web applications. Utilize DataFrame.to_html(classes='table table-striped') and some Javascript to elevate interactivity and BONUS - add your own touch with CSS classes!

Sharing just got easier: Save tables

For sharing your data offline, to_html() lets you save your data to an HTML file:

# "You get a file! You get a file! Everyone gets a file!" df.to_html('my_pretty_table.html')

Keep exploring

Don't settle, always yearn for more. Refer to the References section for official documentation and community guides on advanced printing methods and custom styling techniques.