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_dict={
	'NAME':['Ravi','Raju','Alex'],
	'ID':[1,2,3],'MATH':[30,40,50],
	'ENGLISH':[20,30,41]
	}
my_data = pd.DataFrame(data=my_dict)
my_data.to_html('D:\my_html.html')
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
my_data.to_html('D:\my_html.html',index=False)

Storing Path

We kept the file in D drive ( root )
my_data.to_html('D:\my_file.html')
Inside data directory
my_data.to_html('D:\data\my_file.html')

na_rep Blank data

How to handle if data is blank, we can use na_rep='*'
my_data.to_html('D:\my_file.html',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.read_csv('test.csv')
df=my_data.loc[:,['class','name']]
my_data = pd.DataFrame(data=df)
my_data.to_html('my_file.html',index=False)
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(
      host="localhost",
      user="root",
      passwd="*****",
      database="my_tutorial"
    )
####### end of connection ####
sql="SELECT * FROM student limit 0,5"
my_data = pd.read_sql(sql,my_connect )
my_data.to_html('D:\my_student_file.html',index=False)

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)
df.to_html('D:\\my_data\\my_html.html')

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. 
df.to_html('D:\\my_data\\my_html.html')
We can read one csv file by using read_csv()
df=pd.read_csv("D:\\my_data\\student.csv") # change the path
df.to_html('D:\\my_data\\my_html.html')
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

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    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 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer