The Wayback Machine - https://web.archive.org/web/20240723075312/https://www.geeksforgeeks.org/how-to-render-pandas-dataframe-as-html-table/
Open In App

How to render Pandas DataFrame as HTML Table?

Last Updated : 10 Jan, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Pandas in Python can convert Pandas DataFrame to a table in the HTML web page. pandas.DataFrame.to_html()  method is used to render a Pandas DataFrame. In this article, we will understand how to Styler Object and HTML in Pandas.

Pandas DataFrame as HTML Table Syntax

Syntax: DataFrame.to_html()

Return : Return the html format of a dataframe.

Pandas DataFrame as HTML Table Example

Let’s understand with an example of Styler Object and HTML in Pandas step-by-step.

Step 1: Create a Dataframe

In this example, code uses pandas to create a Pandas DataFrame with details about individuals, such as names, addresses, IDs, and sales. It then prints the original DataFrame in a tabular format using the IPython display function.

Python3




# 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

Here , the code generates an HTML table representation of the DataFrame ‘df’ using to_html and prints the HTML result.

Python3




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 For Convert DataFrame into HTML File

In this example code converts the DataFrame ‘df’ into an HTML table (‘html’) and writes it to an “index.html” file, creating a static HTML page containing the DataFrame representation.

Python3




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:

Step 4: Display HTML Data in the Form of a Table-Stripped

Here code applies Bootstrap styling classes to the DataFrame ‘df’ when converting it to an HTML table, enhancing its visual appearance with striped rows.

Python3




HTML(df.to_html(classes='table table-stripped'))


Output:



Previous Article
Next Article

Similar Reads

Display the Pandas DataFrame in table style and border around the table and not around the rows
Let us see how to style a Pandas DataFrame such that it has a border around the table. We will be using the set_table_styles() method of the Styler class in the Pandas module. set_table_styles() Syntax : set_table_styles(self, table_styles) Parameters : table_styles : List, each individual table_style should be a dictionary with selector and props
1 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()?
We might sometimes need a tidy/long-form of data for data analysis. So, in python's library Pandas there are a few ways to reshape a dataframe which is in wide form into a dataframe in long/tidy form. Here, we will discuss converting data from a wide form into a long-form using the pandas function stack(). stack() mainly stacks the specified index
4 min read
Replace values of a DataFrame with the value of another DataFrame in Pandas
In this article, we will learn how we can replace values of a DataFrame with the value of another DataFrame using pandas. It can be done using the DataFrame.replace() method. It is used to replace a regex, string, list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame method are get replaced with another value dynamically.
4 min read
Converting Pandas Dataframe To Dask Dataframe
In this article, we will delve into the process of converting a Pandas DataFrame to a Dask DataFrame in Python through several straightforward methods. This conversion is particularly crucial when dealing with large datasets, as Dask provides parallel and distributed computing capabilities, allowing for efficient handling of substantial data volume
3 min read
Pandas Dataframe rank() | Rank DataFrame Entries
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas DataFrame rank() method returns a rank of every respective entry (1 through n) along an axis of the DataFrame passed. The rank is retu
3 min read
Pandas DataFrame to_dict() Method | Convert DataFrame to Dictionary
Python is a great language for doing data analysis because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas .to_dict() method is used to convert a DataFrame into a dictionary of series or list-like data type depending on the orient parameter. Exam
3 min read
Python | Pandas DataFrame.fillna() to replace Null values in dataframe
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Just like the pandas dropna() method manages and rem
3 min read
three90RightbarBannerImg