We can create html table output or file by using data from Pandas DataFrame. We will first create one Pandas DataFrame by using some sample data.
https://www.plus2net.com/python/panda...
import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
df = pd.DataFrame(data=my_dict)
After creating the DataFrame we used to_html() to create html tags out of the data
df.to_html()
This output we can use to create file to store the string.
df.to_html('D:\\my_data\\my_html.html')
This will create the my_html.html file . A different path can be given to store the file in different location in the system.
We can add different options while generating the html tags . One important options is columns(), col_space, header, index, justify , max_width etc.
df.to_html(columns=['NAME','ID'])
From MySQL database using read_sql()
We can connect to MySQL database by using SQLAlchemy engine and after connection we will create our DataFrame by using data from sample table by using read_sql.
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")
sql="SELECT * FROM student LIMIT 0,10"
df=pd.read_sql(sql,my_conn)
df.to_html('D:\\my_data\\my_html.html')
Here 10 rows of data from the student table is taken and displayed as string .
Excel or CSV file to string
From excel file we can create string by first creating the dataframe by reading the excel file by using the method read_excel()
df=pd.read_excel("D:\\my_data\\student.xlsx")
df.to_html('D:\\my_data\\my_html.html')
Similarly we can read csv file and generate JSON string
df=pd.read_csv("D:\\my_data\\student.csv")
df.to_html('D:\\my_data\\my_html.html')
#pandas_to_html #dataframetohtml #pandashtml #plus2net #pandas #datascience #pandastutorials
On this page of the site you can watch the video online Python Pandas html output from DataFrame & using MySQL sample table as source by to_html() with a duration of hours minute second in good quality, which was uploaded by the user plus2net 09 May 2021, share the link with friends and acquaintances, this video has already been watched 11,007 times on youtube and it was liked by 129 viewers. Enjoy your viewing!