to_html() : DataFrame to html

Pandas Pandas input output

We will save a DataFrame to .html ( web page ) file. We will create one DataFrame by using a dictionary. From DataFrame we will create the HTML table.
import pandas as pd 
my_data = pd.DataFrame(data=my_dict)
HTML file using DataFrame

Python Pandas html output from DataFrame & using MySQL sample table as source by to_html()

By default we will have index as left most column. We can remove index by using option index=False

Storing Path

We kept the file in D drive ( root )
Inside data directory

na_rep Blank data

How to handle if data is blank, we can use na_rep='*'

Storing part of the data

We can filter the DataFrame and then save the rows in html file. For this we will use our test.csv file as we have more rows.
Now let us store only two columns, class and name
import pandas as pd 
my_data = pd.DataFrame(data=df)
We can use various other filters to manage the data and store in html file. You can rad more on filters sections.

Data from MySQL table to html file

Read on how to connect to MySQL database and then collected the records of student table by using read_sql() to a DataFrame. Finally we will save them in an html file using to_html().
import mysql.connector
import pandas as pd 
my_connect = mysql.connector.connect(
####### end of connection ####
sql="SELECT * FROM student limit 0,5"
my_data = pd.read_sql(sql,my_connect )

Using SQLAlchemy

We will collect records from our sample student table in MySQL database and display as html using to_html().
Collect SQL dump of sample student table below.
Read more on MySQL with SQLAlchemy connection. you can add path if you want the file ( with html tags )to be created with the ( sample is given above )
import pandas as pd 
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student LIMIT 0,10 "
df = pd.read_sql(sql,my_conn)

From Excel file to HTML output

In above code we have created one DataFrame by taking data from a MySQL database table. We can create DataFrame by using any excel data or by using any csv file or from any other sources. ( check here to create a DataFrame from 8 different sources )
Once a DataFrame is created, then using that we can create html output by using to_html(). Here is one example to read one Excel file to a DataFrame and generate the string, you can explore other sources to create a DataFrame and finally generate html tags / file.
We used read_excel() to read our sample student.xlsx file.
df=pd.read_excel("D:\\my_data\\student.xlsx") # Path of the file. 
We can read one csv file by using read_csv()
df=pd.read_csv("D:\\my_data\\student.csv") # change the path
HTML table from MySQL database table

Data input and output from Pandas DataFrame
Pandas read_html() read_csv() read_excel() to_excel()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer