Pretty Printing a pandas DataFrame
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:
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'
:
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:
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:
PrettyTable: ASCII never looked better!
Use prettytable library for a DataFrame converted to an in-memory CSV and printed as an ASCII table:
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:
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.
Was this article helpful?