Pandas DataFrame save as HTML page

Learn, how to convert a Pandas dataframe into HTML page?
Submitted by Pranit Sharma, on November 22, 2022

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.

Converting a Pandas dataframe into HTML page

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.

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 code 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:

Example: Pandas DataFrame save as HTML page

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.