Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas to_html() Method



TheDataFrame.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

TheDataFrame.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 toTrue.

  • index: Whether to include row labels in the output. By default it is set toTrue.

  • 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 toTrue.

  • 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 toTrue.

  • 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

TheDataFrame.to_html() method returns an HTML representation of the DataFrame as a string ifbuf 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 theDataFrame.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 HTMLhtml_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">  <thead>    <tr>      <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 thebuf parameter of theto_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 filedf.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 usingna_rep parameter of theto_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 representationhtml_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">  <thead>    <tr>      <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 theindex=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 indexhtml_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">  <thead>    <tr>      <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

Theto_html() method allows you to control the alignment of column headers using itsjustify 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 alignmenthtml_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">  <thead>    <tr>      <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 theborder andtable_id parameters in theto_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 IDhtml_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">  <thead>    <tr>      <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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp