pandas.DataFrame.to_html#

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)[源代码]#

将 DataFrame 渲染为 HTML 表。

参数:
bufstr, Path 或 StringIO-like, 可选, 默认 None

要写入的缓冲区。如果为 None,则输出将作为字符串返回。

columnsarray-like, optional, default None

要写入的列的子集。默认情况下写入所有列。

col_spacestr or int, list or dict of int or str, optional

每列的最小宽度,以 CSS 长度单位表示。整数被假定为 px 单位。

headerbool, optional

是否打印列标签,默认为 True。

indexbool, optional, default True

是否打印索引(行)标签。

na_repstr, optional, default ‘NaN’

用于表示 NaN 的字符串。

formatterslist, tuple or dict of one-param. functions, optional

用于按位置或名称应用于列元素格式化函数的列表、元组或字典。每个函数的结果必须是 Unicode 字符串。列表/元组的长度必须等于列的数量。

float_formatone-parameter function, optional, default None

如果列元素是浮点数,则应用于这些元素的格式化函数。此函数必须返回一个 Unicode 字符串,并且仅应用于非 NaN 元素,NaN 将由 na_rep 处理。

sparsifybool, optional, default True

对于具有分层索引的 DataFrame,设置为 False 可在每行打印所有多索引键。

index_namesbool, optional, default True

打印索引的名称。

justifystr, default None

如何对齐列标签。如果为 None,则使用打印配置选项(由 set_option 控制),开箱即用为“right”。有效值为:

  • center

  • justify

  • justify-all

  • start

  • end

  • inherit

  • match-parent

  • initial

  • unset.

max_rowsint, optional

在控制台中显示的最大行数。

max_colsint, optional

在控制台中显示的最大列数。

show_dimensionsbool, default False

显示 DataFrame 的维度(行数 x 列数)。

decimalstr, 默认 ‘.’

被识别为小数分隔符的字符,例如欧洲的 ‘,’。

bold_rowsbool, default True

使输出中的行标签加粗。

classesstr or list or tuple, default None

应用于结果 HTML 表的 CSS 类。

escapebool, default True

将字符 <, > 和 & 转换为 HTML 安全序列。

notebook{True, False}, default False

生成的 HTML 是否用于 IPython Notebook。

borderint or bool

当提供整数值时,它将设置起始标签中的 border 属性,指定边框的厚度。如果传递 False0,则 <table> 标签中将不存在 border 属性。此参数的默认值由 pd.options.display.html.border 控制。

table_idstr, optional

如果指定,则 CSS id 会包含在起始 <table> 标签中。

render_linksbool, default False

将 URL 转换为 HTML 链接。

encodingstr, default “utf-8”

设置字符编码。

返回:
str 或 None

如果 buf 为 None,则返回结果作为字符串。否则返回 None。

另请参阅

to_string

将 DataFrame 转换为字符串。

示例

>>> df = pd.DataFrame(data={"col1": [1, 2], "col2": [4, 3]})
>>> html_string = df.to_html()
>>> print(html_string)
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>col1</th>
      <th>col2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>4</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>3</td>
    </tr>
  </tbody>
</table>

HTML 输出

col1

col2

0

1

4

1

2

3

>>> df = pd.DataFrame(data={"col1": [1, 2], "col2": [4, 3]})
>>> html_string = df.to_html(index=False)
>>> print(html_string)
<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>4</td>
    </tr>
    <tr>
      <td>2</td>
      <td>3</td>
    </tr>
  </tbody>
</table>

HTML 输出

col1

col2

1

4

2

3