import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,41]
}
df = pd.DataFrame(data=my_dict) # create DataFrame from dictionary
df.to_html('D:\my_html.html')
df.to_html('D:\my_html.html',index=False)
df.to_html('D:\my_file.html')
Inside data directory
df.to_html('D:\data\my_file.html')
buf
: Buffer to write. columns
: Default value is None, write all columns. The sub list of columns to generate. col_space
: Optional, css width in pixel. Output <th style="min-width: 20px;">header
: Default value is True,bool Optional. To print column headers (True ) or notindex
: Default value is True, bool Optional. To print index row ( True ) or not na_rep
: How to handle NaN enteries. formatters
: Function to apply for column elements for formatting. float_format
: Formatter function for floats. sparsify
: To print hierarchical indexindex_names
: Bool , optional, default True. Print name of Indexes justify
: Justify column labels. Values are left,right,center,justify,justify-all,start,end,inherit,match-parent,initial,unset.max_rows
: int , Optional. Maximum number of rows to display.max_cols
: int , Optional. Maximum number of columns to display. show_dimensions
: bool, default False. decimal
: Decimal separator. Example , is used in Europe. bold_rows
: Bool, default True, mark row labels bold.classes
: (HTML ) Class to add to table for style. escape
: Bool,default True. Converts < , > and & notebook
: Bool, the output is for IPython Notebook.border
: int, border width included in <table> tag, default value is 1, set to 0 to remove border. table_id
: A css id for the html table. render_links
: Default False. Converts URL to Linkencoding
:str,encoding, default 'utf-8' index=False
import pandas as pd
df=pd.read_excel("E:\\data\\student.xlsx") # Path of the file.
l1=['id','name','mark'] # List of columns to show in html table
df.to_html('E:\\data\\my_html.html',index=False,columns=l1)
df.to_html('C:\\data\\student.html',max_rows=5,max_cols=3)
df.to_html('C:\\data\\student.html',index=None,header=False)
df.to_html('C:\\data\\student.html',classes="table table-striped")
<th style="min-width: 50px;">id</th>
df.to_html('C:\\data\\student.html',col_space=50)
import pandas as pd
import numpy as np
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,np.nan,50],
'ENGLISH':[20,30,41]
}
df = pd.DataFrame(data=my_dict) # create DataFrame from dictionary
print(df.to_html(na_rep='*'))
print(df.to_html(show_dimensions=True))
Along with the table we will get output like this.
<p>3 rows × 4 columns</p>
import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,80,50],
'ENGLISH':[20.5,30.65,41.898]
}
df = pd.DataFrame(data=my_dict) # create DataFrame from dictionary
print(df.to_html(decimal=','))
print(df.to_html(table_id='my_table'))
Output
<table border="1" class="dataframe" id="my_table">
import pandas as pd
df=pd.read_csv('test.csv')
df=df.loc[:,['class','name']]
df = pd.DataFrame(data=df)
df.to_html('my_file.html',index=False)
We can use various other filters to manage the data and store in html file. You can read more on filters sections.
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"
df = pd.read_sql(sql,my_connect )
df.to_html('D:\my_student_file.html',index=False)
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:\\df\\my_html.html')
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')
import pandas as pd
df=pd.read_excel("E:\\data\\student.xlsx") # Path of the file.
df.to_html('E:\\data\\my_html.html',classes='center') # File created
Above code will create my_html.html file at the given path by adding the class=center to center allign the table. The top part of the html source is here.
<table border="1" class="dataframe center">
<thead">
<tr style="text-align: right;">
-----
-----
As the class is added to html code, we can define the html class in our style properties. Keep this code in your page CSS or inside HEAD tag. Here is one example.
<style>.center {
margin-left: auto;
margin-right: auto;
}</style>
To print the html output to console.
print(df.to_html(classes='center'))
import pandas as pd
df=pd.read_clipboard(index_col='Value') # use value column as index
df=df.drop(columns='Note') # delete the Note column
print(df.to_html(classes="table table-striped"))
read_clipboard() : Clipboard data to DataFrame
to_html()
?to_html()
function?to_html()
?to_html()
?to_html()
?to_html()
to a file instead of printing it to the console?to_html()
?to_html()
?to_html()
?to_html()
?Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.