Export A Pandas DataFrame Into A HTML Table


  • A pandas DataFrame is a two-dimensional data container for processing voluminous data from various sources.
  • DataFrame can store objects of various Python types.
  • In Data Analytics it is a common requirement to publish final results such as a confusion matrix or a dashboard to an external format like excel, HTML, MySQL and others.
  • The method to_html() of the DataFrame class, returns a HTML string that represents a DataFrame object as a HTML table.


# Example Python program that exports a pandas DataFrame object

# into a HTML table

import pandas as pds


# A Python dictionary representing categories

categories = {"A": [1512, 1245, 1234],

              "B": [1547, 1598, 1345],

              "C": [1676, 1436, 1452]};


# Dictionary loaded into a pandas DataFrame             

dataFrame = pds.DataFrame(data=categories);


# Emit the DataFrame as a HTML string

html = dataFrame.to_html();


# Print the HTML




<table border="1" class="dataframe">


    <tr style="text-align: right;">





























Output as seen in an Internet browser:

Converting a pandas DataFrame object into a HTMLTable


Copyright 2022 © pythontic.com