Home »
Python »
Python Programs
Pandas DataFrame save as HTML page
Learn, how to convert a Pandas dataframe into HTML page?
By Pranit Sharma Last updated : October 05, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
HTML is Hypertext Markup Language; it is also known as the skeletal system of a web page. HTML is used to align and design the elements of a web page for example input forms, tables, lists, etc.
Converting a Pandas dataframe into HTML page
To convert Pandas dataframe into an HTML web page we simply use pandas.DataFrame.to_html() method. This method automatically creates the default HTML code and the plus point is it converts the Pandas dataframe into an HTML table with all the required columns and rows.
Let us understand with the help of an example,
Python program to convert a Pandas dataframe into HTML page
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating dataframe
df = pd.DataFrame({
'A' : ['Hello','World','!'],
'B' : ['This','is','Includehelp']
})
# Display original DataFrame
print("Original DataFrame:\n",df,"\n")
# Using .to_html
res = df.to_html('df.html')
# Display a msg
print("HTML file created")
Output
The output of the above program is:
Python Pandas Programs »