Pandas DataFrame to_html() Method

Rate this post


Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Openpyxl library enables conversion to/from Excel.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install openpyxl

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required libraries.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import openpyxl


The to_html() method converts a valid DataFrame object to an HTML Table format.

DataFrame.to_html(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.', bold_rows=True, classes=None, escape=True, notebook=False, border=None, table_id=None, render_links=False, encoding=None)
bufThis parameter is the buffer to write to. If empty, a string returns.
columnsThe column subset to convert. If empty, all columns will convert.
col_spaceThe minimum width (CSS length) of each column.
headerIf True, the column heads will display.
indexIf True, the row labels will display.
na_repThe string depiction of any NaN values.
formattersThe formatter is the option to apply to each column (name/position).
float_formatThe formatter option to apply to float values.
sparsifyFor MultiIndex DataFrames. Set to False for a hierarchical index.
index_namesIf True, the index names display.
justifyA string value that depicts the justification type. Options are:
left, right, center, justify, justify-all, start, end, inherit,
match-parent, initial and, unset.
max_rowsThe maximum number of rows to display.
max_colsThe maximum number of columns to display
show_dimensionsDisplay the dimensions of the DataFrame (rows/columns).
decimalThis parameter is the character for a comma (,) in Europe.
bold_rowsIf True, the row labels will be bold.
classesClasses (formatting) to apply to the HTML table.
escapeConvert the characters: <>& to HTML-safe characters.
notebookThis parameter determines if the output is for an ipython notebook.
borderIf True, a border surrounds the cells of each column/row in the HTML table.
table_idIf set, a CSS id tag (#) is included in the opening <table> tag.
render_linksIf True, this parameter converts URLs strings to links.
encodingThis parameter sets the encoding for the HTML.

This example reads in the countries.csv file and converts it to an HTML table. To follow along, click here to save this CSV file and move it to the current working directory.

πŸ’‘ Note: To run this code, copy the code to the clipboard. Click here to navigate to a Jupyter testing environment. If this code is run in a standard IDE, the formatting will not display.

from IPython.display import HTML

df = pd.DataFrame({'Tops':     [36, 23, 19],
                   'Tanks':    [20, 10, 20],
                   'Pants':    [61, 33, 67],
                   'Sweats':   [88, 38, 13]})

HTML(df.to_html(classes='table table-hover'))
  • Line [1] imports the appropriate library to render the HTML.
  • Line [2] creates a DataFrame from a dictionary of lists. The output saves to df.
  • Line [3] exports as HTML with formatting.


More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.