Render Pandas DataFrame as HTML Table - Python Examples
Pandas - Render DataFrame as HTML Table
You can convert a Pandas DataFrame into an HTML table to represent it on web pages.
To render a Pandas DataFrame as an HTML Table, use the pandas.DataFrame.to_html()
method.
The entire DataFrame is converted into a <table>
HTML element, with column names wrapped in the <thead>
(table head) element. Each row of the DataFrame is then represented as a <tr>
(table row) in the HTML table.
Examples
1. Render DataFrame as HTML Table
In this example, we will initialize a DataFrame and render it into an HTML table.
Python Program
import pandas as pd
# Create DataFrame
df_marks = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
'physics': [68, 74, 77, 78],
'chemistry': [84, 56, 73, 69],
'algebra': [78, 88, 82, 87]})
# Render DataFrame as HTML
html = df_marks.to_html()
print(html)
Explanation:
- The DataFrame is created using a dictionary containing student names and their marks in various subjects.
- The
to_html()
method is called on the DataFrame, converting it into an HTML table. - The resulting HTML table includes the column names as table headers and each row of data as a table row.
Output
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>name</th>
<th>physics</th>
<th>chemistry</th>
<th>algebra</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Somu</td>
<td>68</td>
<td>84</td>
<td>78</td>
</tr>
<tr>
<th>1</th>
<td>Kiku</td>
<td>74</td>
<td>56</td>
<td>88</td>
</tr>
<tr>
<th>2</th>
<td>Amol</td>
<td>77</td>
<td>73</td>
<td>82</td>
</tr>
<tr>
<th>3</th>
<td>Lini</td>
<td>78</td>
<td>69</td>
<td>87</td>
</tr>
</tbody>
</table>
Let us now write the HTML data to a file using Python.
Python Program
import pandas as pd
#create dataframe
df_marks = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
'physics': [68, 74, 77, 78],
'chemistry': [84, 56, 73, 69],
'algebra': [78, 88, 82, 87]})
#render dataframe as html
html = df_marks.to_html()
#write html to file
with open("index.html", "w") as text_file:
text_file.write(html)
This will create an index.html
file in your working directory containing the HTML table. Open this file in a web browser to view the table.
2. Render DataFrame with Customizing Table Attributes
You can customize the appearance of the HTML table by passing additional parameters to the to_html()
method, such as adding borders, aligning content, or using custom classes for styling.
Python Program
import pandas as pd
# Create DataFrame
df_marks = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
'physics': [68, 74, 77, 78],
'chemistry': [84, 56, 73, 69],
'algebra': [78, 88, 82, 87]})
# Render DataFrame as HTML with custom styles
html = df_marks.to_html(border=2, justify='center', classes='styled-table')
# Write HTML to file
with open('styled_table.html', 'w') as text_file:
text_file.write(html)
Explanation:
- The
border
attribute adds a border around the table, thejustify
option centers the table, and theclasses
attribute adds a custom class for styling. - This HTML table can now be styled using the specified class (e.g., in a CSS file or inline). This makes it more flexible for styling when integrated into a web page.
Output
<table border="2" class="styled-table">
<thead>
<tr style="text-align: center;">
<th></th>
<th>name</th>
<th>physics</th>
<th>chemistry</th>
<th>algebra</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Somu</td>
<td>68</td>
<td>84</td>
<td>78</td>
</tr>
<tr>
<th>1</th>
<td>Kiku</td>
<td>74</td>
<td>56</td>
<td>88</td>
</tr>
<tr>
<th>2</th>
<td>Amol</td>
<td>77</td>
<td>73</td>
<td>82</td>
</tr>
<tr>
<th>3</th>
<td>Lini</td>
<td>78</td>
<td>69</td>
<td>87</td>
</tr>
</tbody>
</table>
3. Render DataFrame with Missing Values
If your DataFrame contains missing values, NaN
will be displayed in the table.
Python Program
import pandas as pd
# Create DataFrame with NaN values
df_marks = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
'physics': [68, 74, None, 78],
'chemistry': [84, None, 73, 69],
'algebra': [78, 88, None, 87]})
# Render DataFrame with NaN values
html = df_marks.to_html()
# Write HTML to file
with open('nan_table.html', 'w') as text_file:
text_file.write(html)
Explanation:
- The DataFrame contains missing values, represented by
None
in Python, which will be shown asNaN
in the HTML table. - This is useful for displaying incomplete data or handling missing information in a dataset.
Output
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>name</th>
<th>physics</th>
<th>chemistry</th>
<th>algebra</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Somu</td>
<td>68</td>
<td>84</td>
<td>78</td>
</tr>
<tr>
<th>1</th>
<td>Kiku</td>
<td>74</td>
<td>NaN</td>
<td>88</td>
</tr>
<tr>
<th>2</th>
<td>Amol</td>
<td>NaN</td>
<td>73</td>
<td>NaN</td>
</tr>
<tr>
<th>3</th>
<td>Lini</td>
<td>78</td>
<td>69</td>
<td>87</td>
</tr>
</tbody>
</table>
Summary
In this tutorial, we have learned how to render a Pandas DataFrame as an HTML table using the to_html()
method. We covered basic usage as well as advanced customizations like adding borders, aligning the table, and handling missing data.
For more examples, visit the Pandas Tutorial.