How to render Pandas DataFrame as HTML Table?

How to render Pandas DataFrame as HTML Table?


Pandas in Python has the ability to convert Pandas DataFrame to a table in the HTML web page. pandas.DataFrame.to_html()  method is used for render a Pandas DataFrame.

Syntax : DataFrame.to_html()
Return : Return the html format of a dataframe.

Let’s understand with examples:

First, create a Dataframe:

# importing pandas as pd
import pandas as pd
from IPython.display import HTML
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham'
                            'Saurabh', 'Ujjawal'],
                   "Address": ['Patna', 'Delhi', 'Coimbatore',
                               'Greater noida', 'Patna'],
                   "ID": [20123, 20124, 20145, 20146, 20147],
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
print("Original DataFrame :")


Convert Dataframe to Html table:

result = df.to_html()


<table border="1" class="dataframe">
    <tr style="text-align: right;">
      <td>Greater noida</td>

Let write the script for convert DataFrame into HTML file:

html = df.to_html()
# write html to file
text_file = open("index.html", "w")

Note: The HTML file will be created with HTML data in the current working directory.



Let’s display HTML data in the form of a table-striped

HTML(df.to_html(classes='table table-striped'))


Last Updated on August 30, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs