Pandas in Python can alter a Pandas DataFrame into an HTML table. A Pandas DataFrame is executed using the “pandas.DataFrame.to_html()” method. Let’s look at our example and discuss the procedure to transform our Python DataFrame to HTML source code. To accomplish this, we must first design the DataFrame that ultimately renders into HTML.
Syntax:
Parameters:
-
- If “index” is set to True, the HTML output contains the index for each row. Otherwise, no index is present if it is set to False. By default, it is True.
- The max_rows takes an integer value that converts the specified number of rows into an HTML format.
- The max_cols takes an integer value that converts the specified number of columns into an HTML format.
Return Format:
The entire data is placed under the table tag.
The index values are placed under the “<th>” tag. The row values are placed under the “<td>” tag which is under the “<th>” column.
Example 1: With No Parameter
In this example, we have a DataFrame named “things” that holds the “Product”, ”Name”, and “Purchased Status” columns with 2 rows.
Convert this DataFrame to HTML by passing no parameter.
# Create a pandas dataframe with 3 columns.
things=pandas.DataFrame({'Product':[1,2],
'Name':['Solar dish','glasses'],
'Purchased Status':[1,0]},index=['one','two'])
print(things)
print()
# Convert things DataFrame to html
print(things.to_html())
Output:
one 1 Solar dish 1
two 2 glasses 0
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Product</th>
<th>Name</th>
<th>Purchased Status</th>
</tr>
</thead>
<tbody>
<tr>
<th>one</th>
<td>1</td>
<td>Solar dish</td>
<td>1</td>
</tr>
<tr>
<th>two</th>
<td>2</td>
<td>glasses</td>
<td>0</td>
</tr>
</tbody>
</table>
Explanation:
In the first output, we displayed the actual DataFrame. In the second output, our DataFrame is displayed in HTML format.
Let’s run the HTML code output in the browser.
Example 2: With the Index Parameter
Convert this DataFrame to HTML by ignoring the index. Here, the index is set to False.
things=pandas.DataFrame({'Product':[1,2],
'Name':['Solar dish','glasses'],
'Purchased Status':[1,0]},index=['one','two'])
# Convert to html without index
print(things.to_html(index=False))
Output:
<thead>
<tr style="text-align: right;">
<th>Product</th>
<th>Name</th>
<th>Purchased Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Solar dish</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>glasses</td>
<td>0</td>
</tr>
</tbody>
</table>
Explanation:
Our DataFrame is displayed in HTML format without the index values.
Let’s run the HTML code output in the browser.
You can see that the index values were not found in the HTML table.
Example 3: With the Max_Rows Parameter
Convert only the first row of the DataFrame to HTML by setting the max_rows parameter to 1.
things=pandas.DataFrame({'Product':[1,2],
'Name':['Solar dish','glasses'],
'Purchased Status':[1,0]})
# Convert to html with one row.
print(things.to_html(max_rows=1))
Output:
<thead>
<tr style="text-align: right;">
<th></th>
<th>Product</th>
<th>Name</th>
<th>Purchased Status</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>Solar dish</td>
<td>1</td>
</tr>
</tbody>
</table>
Explanation:
Our DataFrame is displayed in HTML format that has only one row.
Let’s run the HTML code output in the browser.
Example 4: With the Max_Cols Parameter
Convert only the first column of the DataFrame to HTML by setting the max_cols parameter to 1.
things=pandas.DataFrame({'Product':[1,2],
'Name':['Solar dish','glasses'],
'Purchased Status':[1,0]})
# Convert to html with one column.
print(things.to_html(max_cols=1))
Output:
<thead>
<tr style="text-align: right;">
<th></th>
<th>Product</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>...</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>...</td>
</tr>
</tbody>
</table>
Explanation:
Our DataFrame is displayed in HTML format that has only one column.
Let’s run the HTML code output in the browser.
Conclusion
When rendering a DataFrame into an HTML code, we use the things.to_html() function. Also, we convert the existing DataFrame by setting the index, max_rows and max_cols parameters in separate examples. For all the outputs, we displayed the HTML actual data by providing a screenshot after every output.