HTML is a language for making web pages with tags that define the structure and content, such as headings, paragraphs, tables, images, and links. It is used to show information on the internet and to interact with web browsers. A DataFrame is a two-dimensional data structure in Pandas, a Python library for data calculation and manipulation. We may want to make HTML tables from DataFrames for display on a web page or to save them as HTML files for future use. To create HTML from DataFrames the “DataFrame.to_html()” method is utilized in Python.
This guide will present you with a detailed tutorial on converting Pandas to HTML using numerous examples via the below content:
What is the “DataFrame.to_html()” Method in Python?
The “DataFrame.to_html()” method of the “Pandas” library is utilized to transform a DataFrame of Pandas into an HTML table format. This method takes no arguments by default, but it can also take an optional argument that can be used to customize the output.
Syntax
Parameters
In the above syntax:
-
- The “buf” parameter value is None (default) then the HTML is returned as a string. Otherwise, it is written to the buffer object specified by buf.
- The “columns” parameter is a list or array of column labels/names to add/include in the result. If None (default), all columns are included.
- The “col_space” parameter represents the number of spaces to add between columns. If None (default), the value is inferred from the DataFrame’s column spacing.
- The “header” parameter specifies whether to include the column headers (default True).
- The “index” parameter indicates whether to include the row indexes (default True).
- The “na_rep” parameter specifies whether the string is used for missing values. Defaults to “NaN”.
Follow this documentation for a detailed overview of the syntax.
Return Value
The “DataFrame.to_html()” method retrieves string or none. If buf is None, return the output as a string. Otherwise returns None.
Example 1: Convert DataFrame to HTML Table Using “DataFrame.to_html()” Method
In the following code, we first create the DataFrame with some column values. Next, we use the “df.to_html()” method of the Pandas module to convert the DataFrame into HTML table format:
df = pandas.DataFrame({"Name": ['Joseph', 'Anna', 'Henry'],
"Age": [22, 19, 24],"Height": [5.3, 4.6, 5.2]})
print(df.to_html())
The execution of the above code retrieves the below output:
Example 2: Convert DataFrame to HTML File Using “DataFrame.to_html()” Method
We can also convert the DataFrame to an HTML file using the “DataFrame.to_html()” method and “write()” methods. First, we need to create the DataFrame and convert it into “HTML” format. After that, the new file is opened in writing mode and the HTML format data is written to the file. The “f.write()” method is used to write the HTML table format data to the HTML file:
df = pandas.DataFrame({"Name": ['Joseph', 'Anna', 'Henry'],
"Age": [22, 19, 24],"Height": [5.3, 4.6, 5.2]})
html = df.to_html()
f = open("index.html", "w")
f.write(html)
f.close()
The below snippet shows that the “HTML” file has been created successfully:
The following snippet shows the content of the newly created HTML file:
Example 3: Converting Selected Columns of DataFrame to HTML Using “DataFrame.to_html()” Method
Here, we can use the “columns=” parameter of the “DataFrame.to_html()” method to convert the selected columns of DataFrame into HTML format. The “Name” and “Height” column label is passed as a list to the “columns=” parameter:
df = pandas.DataFrame({"Name": ['Joseph', 'Anna', 'Henry'],
"Age": [22, 19, 24],"Height": [5.3, 4.6, 5.2]})
print(df.to_html(columns=['Name', 'Height']))
The specified column of DataFrame has been converted into an HTML table:
The below snippet shows the content when the above HTML code is opened as a file:
Example 4: Converting DataFrame to HTML and Change Column Width Using “DataFrame.to_html()” Method
There are a number of parameters that can be used for customizing HTML data. In the below code, the “col_space=” parameter with integer value is passed to the “df.to_html()” method for converting the DataFrame of Pandas to HTML.
df = pandas.DataFrame({"Name": ['Joseph', 'Anna', 'Henry'],
"Age": [22, 19, 24],"Height": [5.3, 4.6, 5.2]})
print(df.to_html(col_space = 55))
The below snippet shows the HTML file content:
Conclusion
In Python, the “DataFrame.to_html()” method is utilized to convert the entire or selected DataFrame into an HTML table format. We can also customize the converted HTML file by utilizing several parameters of the “DataFrame.to_html()” method. To create the HTML file from the DataFrame the “df.to_html()” method is used along with the “open()” and “write()” method. This tutorial covered a comprehensive guide on Pandas to HTML conversion utilizing considerable examples.