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> 
python_pandas_io_tool.htm
Advertisements
close