How to render Pandas DataFrame as HTML Table? (original) (raw)
Last Updated : 02 Jan, 2025
Pandas in Python can convert a Pandas DataFrame to a table in an HTML web page. The pandas.DataFrame.to_html() method is used to render a Pandas DataFrame into an HTML format, allowing for easy display of data in web applications.
In this article, we will understand how to use the **Styler Object and HTML in Pandas, specifically focusing on the process of converting a **dataframe to HTML.
- We will explore how to create a **dataframe to HTML table in Python and utilize the **pandas to_html method to generate styled HTML tables.
- Additionally, we will look at ways to enhance the visual presentation of data by leveraging the **pandas to HTML table functionalities.
Table of Content
Pandas DataFrame : The to_html()
Function
Syntax: DataFrame.to_html()
Return : Return the html format of a dataframe.
**Return: This function returns the HTML format of a DataFrame, enabling seamless integration of a **Pandas DataFrame to HTML for display in web applications. The **pandas to_html method is essential for converting data into an easily readable format, allowing users to create a **dataframe to HTML table in Python effortlessly.
Pandas DataFrame as HTML Table Example
Let’s understand the process of converting a **Pandas DataFrame to an HTML table with an example of the **Styler Object and HTML in Pandas step-by-step.
Step 1: **Create a Dataframe
In this example, we will use **pandas to create a **Pandas DataFrame with details about individuals, such as names, addresses, IDs, and sales. The code will then print the original DataFrame in a tabular format using the IPython display function. This example illustrates how to generate a **dataframe to HTML for easy visualization on web pages.
Python `
importing pandas as pd
import pandas as pd from IPython.display import HTML
creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham', 'Saurabh', 'Ujjawal'],
"Address": ['Patna', 'Delhi', 'Coimbatore',
'Greater noida', 'Patna'],
"ID": [20123, 20124, 20145, 20146, 20147],
"Sell": [140000, 300000, 600000, 200000, 600000]})
print("Original DataFrame :") display(df)
`
Output:
**Original DataFrame :
**Name Address ID Sell
0 Anurag Patna 20123 140000
1 Manjeet Delhi 20124 300000
2 Shubham Coimbatore 20145 600000
3 Saurabh Greater noida 20146 200000
4 Ujjawal Patna 20147 600000
Step 2: Convert Dataframe to Html Table
In this step, the code generates an HTML table representation of the DataFrame ‘df’ using the to_html()
function. This method allows you to convert a **Pandas DataFrame to HTML, producing a well-structured **dataframe to HTML table in Python. The generated HTML result can be printed or embedded in a web page for better data presentation.
By utilizing the **pandas to_html method, you can easily create a **pandas to HTML table, enhancing the visual appeal and accessibility of your data.
Python `
result = df.to_html() print(result)
`
Output:
Name | Address | ID | Sell | |
---|---|---|---|---|
0 | Anurag | Patna | 20123 | 140000 |
1 | Manjeet | Delhi | 20124 | 300000 |
2 | Shubham | Coimbatore | 20145 | 600000 |
3 | Saurabh | Greater noida | 20146 | 200000 |
4 | Ujjawal | Patna | 20147 | 600000 |
Step 3: Write the Script to Convert DataFrame into HTML File
In this example, the code converts the DataFrame df
into an HTML table using the to_html()
method and writes it to an “index.html” file. This process creates a static HTML page containing the DataFrame representation, effectively showcasing how to use **pandas to_html to generate an easily viewable format for web applications.
Python `
html = df.to_html()
write html to file
text_file = open("index.html", "w") text_file.write(html) text_file.close()
`
**Note: The HTML file will be created with HTML data in the current working directory.
Output:
This script illustrates the conversion of a DataFrame to HTML, showcasing how to create a **dataframe to HTML table in Python and produce an HTML file that presents the data as a **pandas to HTML table.
**Step 4: Display HTML Data in the Form of a Table-Stripped
In this step, the code applies Bootstrap styling classes to the DataFrame df
when converting it to an HTML table, enhancing its visual appearance with striped rows. By utilizing the **pandas to_html method, you can generate a **dataframe to HTML table in Python that is not only functional but also aesthetically pleasing.
The application of Bootstrap classes allows for the creation of a well-formatted **Pandas DataFrame to HTML display, making it easier to read and interact with. This approach exemplifies how to effectively use the **pandas to HTML table functionalities while enhancing the user experience.
Python `
HTML(df.to_html(classes='table table-stripped'))
`
Output:
Conclusion
In summary, converting a **Pandas DataFrame to HTML using the pandas.DataFrame.to_html()
method is a powerful way to present data in web applications. This functionality allows for seamless integration of **dataframe to HTML formats, making it easy to create a **dataframe to HTML table in Python. Additionally, by applying CSS frameworks like Bootstrap, you can enhance the visual appeal of your **pandas to HTML table, resulting in a more user-friendly interface. Understanding how to effectively utilize the **pandas to_html method is essential for any data scientist or developer looking to display data dynamically on the web.