
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas to_html() Method
The DataFrame.to_html() method in Python's Pandas library allows you to convert a Pandas DataFrame into an HTML table representation. This is especially useful for exporting data for web display or integrating data analysis results into web applications.
The method provides various customization options, such as controlling column visibility, formatting specific values, and adjusting the table's appearance with attributes like borders, CSS classes, and alignment. Additionally, it allows handling missing data, adding links, and defining formatting rules for numeric values.
Syntax
The syntax of the DataFrame.to_html() method is as follows −
DataFrame.to_html(buf=None, *, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.', bold_rows=True, classes=None, escape=True, notebook=False, border=None, table_id=None, render_links=False, encoding=None)
Parameters
The DataFrame.to_html() method accepts the following parameters −
buf: This parameter accepts a string, path object, or file-like object representing the file location. If not provided, the output is returned as a string.
columns: Specifies a subset of columns to include in the output HTML. By default, all columns are included.
col_space: Specifies the minimum width of each column in CSS units.
header: Specifies whether to include column labels in the output. By default it is set to True.
index: Whether to include row labels in the output. By default it is set to True.
na_rep: String representation for missing values. By default missing values are represented with 'NaN'.
formatters: A list, tuple, or dictionary of functions to format the column values.
float_format: A function to format floating-point numbers.
sparsify: Whether to sparsify hierarchical index values. By default it is set to True.
index_names: Whether to include index names in the output.
justify: A string specifies the alignment for column labels. Available options are left, right, center, justify, justify-all, start, end, inherit, match-parent, initial, and unset.
max_rows: Maximum number of rows to include.
max_cols: Maximum number of columns to include.
show_dimensions: Whether to display DataFrame dimensions.
decimal: Character to use as the decimal separator.
bold_rows: Whether to bold row labels (default: True).
classes: CSS classes to apply to the output HTML table.
escape: Whether to convert the characters <, > and & to HTML safe sequences. By default set to True.
notebook: Determines whether the output is generated for an IPython notebook.
border: Adds a border attribute to the table.
table_id: Adds a CSS ID to the table.
render_links: Converts URLs to HTML links.
encoding: Character encoding for the output.
Return Value
The DataFrame.to_html() method returns an HTML representation of the DataFrame as a string if buf is not provided. Otherwise, it writes the output to the specified buffer.
Example: Exporting DataFrame as an HTML
Here is a basic example of using the DataFrame.to_html() method to render a simple DataFrame as an HTML string with default settings.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col1": [1, 2, 3], "Col2": ["a", "b", "c"]}, index=["r1", "r2", "r3"]) # Convert DataFrame to HTML html_output = df.to_html() print('Output HTML:') print(html_output)
When we run the above program, it produces the following result −
Output HTML: <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Col1</th> <th>Col2</th> </tr> </thead> <tbody> <tr> <th>r1</th> <td>1</td> <td>a</td> </tr> <tr> <th>r2</th> <td>2</td> <td>b</td> </tr> <tr> <th>r3</th> <td>3</td> <td>c</td> </tr> </tbody> </table>
Example: Exporting DataFrame to an HTML File
Here is an example that exports a Pandas DataFrame to an HTML file, by specifying the file path to the buf parameter of the to_html() method. Instead of displaying the HTML string like the previous example, this will save the data into an HTML file.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col1": [1, 2, 3], "Col2": ["a", "b", "c"]}, index=["r1", "r2", "r3"]) # Export DataFrame to an HTML file df.to_html('output.html') print("DataFrame has been exported to 'output.html'.")
The following is the output of the above code −
DataFrame has been exported to 'output.html'.
Example: Customizing Missing Value Representation
This example customizes the representation of missing values in the output HTML using na_rep parameter of the to_html() method. Here, we replace missing values in the output HTML with a custom string ("--").
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col1": [1, None, 3], "Col2": ["a", "b", None]}, index=["r1", "r2", "r3"]) # Customize missing value representation html_output = df.to_html(na_rep='--') print('Output HTML:') print(html_output)
Output of the above code is as follows −
Output HTML: <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Col1</th> <th>Col2</th> </tr> </thead> <tbody> <tr> <th>r1</th> <td>1.0</td> <td>a</td> </tr> <tr> <th>r2</th> <td>--</td> <td>b</td> </tr> <tr> <th>r3</th> <td>3.0</td> <td>None</td> </tr> </tbody> </table>
Example: Hiding the Index in the HTML Table
By default, Pandas includes the index in the output HTML table. You can remove it by using the index=False parameter. The following example demonstrates the same −
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col1": [1, 2, 3], "Col2": ["a", "b", "c"]}, index=["r1", "r2", "r3"]) # Convert DataFrame to HTML without index html_output = df.to_html(index=False) print('Output HTML:') print(html_output)
The output for the above code hides the index labels −
Output HTML: <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th>Col1</th> <th>Col2</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>a</td> </tr> <tr> <td>2</td> <td>b</td> </tr> <tr> <td>3</td> <td>c</td> </tr> </tbody> </table>
Example: Customizing the HTML Table Column Alignment
The to_html() method allows you to control the alignment of column headers using its justify parameter. In the following example the column labels are aligned to the center.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col1": [1, 2, 3], "Col2": ["a", "b", "c"]}, index=["r1", "r2", "r3"]) # Convert DataFrame to HTML with custom column alignment html_output = df.to_html(justify='center') print('Output HTML:') print(html_output)
Output of the above code is as follows −
Output HTML: <table border="1" class="dataframe"> <thead> <tr style="text-align: center;"> <th></th> <th>Col1</th> <th>Col2</th> </tr> </thead> <tbody> <tr> <th>r1</th> <td>1</td> <td>a</td> </tr> <tr> <th>r2</th> <td>2</td> <td>b</td> </tr> <tr> <th>r3</th> <td>3</td> <td>c</td> </tr> </tbody> </table>
Example: Adding a Border and Custom Table ID to The HTML Table
Here is an example that demonstrating the use of the border and table_id parameters in the to_html() method for specifying a border for the table and add a custom id attribute for styling with CSS.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col1": [1, 2, 3], "Col2": ["a", "b", "c"]}, index=["r1", "r2", "r3"]) # Convert DataFrame to HTML with a border and custom table ID html_output = df.to_html(border=2, table_id="student_table") print('Output HTML:') print(html_output)
The following is the output of the above code −
Output HTML: <table border="2" class="dataframe" id="student_table"> <thead> <tr style="text-align: right;"> <th></th> <th>Col1</th> <th>Col2</th> </tr> </thead> <tbody> <tr> <th>r1</th> <td>1</td> <td>a</td> </tr> <tr> <th>r2</th> <td>2</td> <td>b</td> </tr> <tr> <th>r3</th> <td>3</td> <td>c</td> </tr> </tbody> </table>