Open In App

How to render Pandas DataFrame as HTML Table?

Last Updated : 02 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

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.

# 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.

result = df.to_html()
print(result)

Output:

<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Name</th>
<th>Address</th>
<th>ID</th>
<th>Sell</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Anurag</td>
<td>Patna</td>
<td>20123</td>
<td>140000</td>
</tr>
<tr>
<th>1</th>
<td>Manjeet</td>
<td>Delhi</td>
<td>20124</td>
<td>300000</td>
</tr>
<tr>
<th>2</th>
<td>Shubham</td>
<td>Coimbatore</td>
<td>20145</td>
<td>600000</td>
</tr>
<tr>
<th>3</th>
<td>Saurabh</td>
<td>Greater noida</td>
<td>20146</td>
<td>200000</td>
</tr>
<tr>
<th>4</th>
<td>Ujjawal</td>
<td>Patna</td>
<td>20147</td>
<td>600000</td>
</tr>
</tbody>
</table>

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.

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.

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.

Converting Pandas DataFrame to HTML – FAQs

What is the purpose of the to_html() function in Pandas?

The to_html() function in Pandas is used to convert a DataFrame into HTML format, allowing users to easily display data in web applications. This function facilitates the creation of a dataframe to HTML table that can be embedded in web pages.

How can I create a dataframe to HTML table in Python?

To create a dataframe to HTML table in Python, you can use the pandas.DataFrame.to_html() method. This method converts the DataFrame into an HTML table format, which can be styled and displayed in a web application.

Can I apply CSS styles to a pandas to_html table?

Yes, you can apply CSS styles to a pandas to HTML table by adding Bootstrap classes or custom CSS to enhance the appearance of the table. This can improve the visual layout and readability of your data presentation.

What options are available when using the pandas to_html method?

The pandas to_html method offers several options, such as specifying the table attributes, customizing the table format, and selecting which columns or rows to display. This flexibility allows for tailored presentations of data.

Is it possible to export a Pandas DataFrame directly to HTML?

Yes, you can directly export a Pandas DataFrame to HTML using the to_html() function. This makes it easy to generate a dataframe to HTML representation of your data without additional steps.

How do I customize the appearance of the generated HTML table?

You can customize the appearance of the generated HTML table by using the classes parameter in the to_html() method to apply CSS classes, such as those from Bootstrap, or by directly modifying the HTML output with custom styles.



Next Article

Similar Reads

three90RightbarBannerImg