to_html()

Pandas

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
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)
HTML table from MySQL database table

Data input and output from Pandas DataFrame
Pandas read_html() read_csv() read_excel() to_excel()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer