參數(shù)
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,
bold_rows=True,classes=None, escape=True, max_rows=None, max_cols=None,show_dimensions=False,
notebook=False, decimal='.', border=None)
buf : StringIO-like, 可選
寫入緩沖區(qū)浩螺。
columns : sequence拉盾,可選
要轉(zhuǎn)化的列的列名桨菜;默認(rèn)值 None 為所有列轉(zhuǎn)化。
col_space : int捉偏,可選
每列的最小寬度倒得。
header : bool,可選
是否打印列標(biāo)簽夭禽,默認(rèn)為 True霞掺。
index : 布爾值,可選
是否打印索引(行)標(biāo)簽讹躯,默認(rèn)為 True菩彬。
na_rep : 字符串,可選
指定 NAN 的字符串表示形式潮梯,默認(rèn)為 'NaN'骗灶。
formatters : 多個(gè)單參數(shù)函數(shù)組成的列表或字典,可選
格式化程序可按列表的所索引或字典的鍵名稱應(yīng)用于列元素秉馏,默認(rèn)為 None耙旦。
每個(gè)單參數(shù)函數(shù)的結(jié)果必須是一個(gè) unicode 字符串。列表的長(zhǎng)度必須等于列數(shù)萝究。
float_format: 單參數(shù)函數(shù)免都,可選
用于將列元素設(shè)置為浮點(diǎn)數(shù)的格式化程序功能,默認(rèn)為無帆竹。
此單參數(shù)函數(shù)的結(jié)果必須是 unicode 字符串绕娘。
sparsify : bool,可選
默認(rèn)為 True馆揉。輸入 False 時(shí)业舍,對(duì)于具有層次結(jié)構(gòu)索引的 DataFrame,會(huì)在每一行打印多重索引升酣。
index_names : bool舷暮,可選
打印索引名稱,默認(rèn)為 True噩茄。
line_width : int下面,可選
換行符的寬度,默認(rèn)為不換行绩聘。
justify : 列標(biāo)簽對(duì)齊方式, 可選
左右對(duì)齊列標(biāo)簽沥割。默認(rèn)為 None時(shí)耗啦,使用打印配置中的選項(xiàng)(由 set_option 控制),則右對(duì)齊机杜。
bold_rows : bool, 可選
對(duì)橫向表格線進(jìn)行加粗帜讲。
classes : CSS類(es)適用于生成的html表, 可選
默認(rèn) None
escape : bool, 可選
將 "<", ">", "&" 轉(zhuǎn)化成 html 安全序列(?椒拗?)似将,默認(rèn) True。
max_rows : int, 可選
顯示最大行數(shù)蚀苛。
max_cols : int, 可選
顯示最大列數(shù)在验。
decimal : str, 可選
小數(shù)分隔符, 默認(rèn)為 '.'。
border : int堵未, 可選
表格外邊框?qū)挾纫干啵J(rèn)為 1,參數(shù)為 0 時(shí)表格無邊框渗蟹。數(shù)值越大外邊框越寬块饺。
示例
默認(rèn)格式
data = [{"col_a":"AAAAAAAAAAAAAAAA", "col_b":"1.22288", "col_c":"1", "col_d":None},
{"col_a":"HHHH", "col_b":"2.55566", "col_c":np.nan, "col_d":{
"idx_1":"dark", "idx_2":"light"}
}]
df_test = pd.DataFrame(data)
html_text = df_test.to_html()
行索引
html_text2 = df_test.to_html(index=False) # 無行索引
邊框?qū)挾?/p>
html_text3 = df_test.to_html(border=5) # 邊框?qū)挾仍O(shè)為 2, 設(shè)為 0 則 無邊框
對(duì)齊方式
# justify='left' 為左對(duì)齊, justify='right' 為右對(duì)齊
html_text4 = df_test.to_html(index=False, justify='center') # 列標(biāo)簽內(nèi)容居中對(duì)齊
空的顯示
html_text5 = df_test.to_html(index=False, na_rep="空") # 自定義空字符表現(xiàn)形式
- 測(cè)試可知,
na_rep
參數(shù)設(shè)定對(duì)None
不起作用拙徽。
邊框線實(shí)心
html_text6 = df_test.to_html()
print(html_text6)
"""
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
...
...
...
</tr>
</tbody>
</table>
"""
to_html()
我沒有找到讓邊框線變實(shí)心的功能刨沦。但我們可以用嘗試改造 html
文本。我們看下 html_text6
的 html
文本膘怕,html
表格的屬性存放在 table
標(biāo)簽中,我們可以在 class
前面插入邊框線實(shí)心的參數(shù)召庞。
html_text7 = html_text6.replace('class', 'cellspacing=\"0\" class')
print(html_text7)
"""
<table border="1" cellspacing="0" class="dataframe">
<thead>
<tr style="text-align: right;">
...
...
...
</tr>
</tbody>
</table>
"""
表格標(biāo)題
title = "測(cè)試表格標(biāo)題"
html_text8 = html_text7.replace('class="dataframe">',
'class="dataframe"><caption>{}</caption>'.format(title)
)
單元格顏色
print(html_text8)
"""
<table border="1" class="dataframe"><caption>測(cè)試表格標(biāo)題</caption>
<thead>
<tr style="text-align: right;">
<th></th>
<th>col_a</th>
<th>col_b</th>
<th>col_c</th>
<th>col_d</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>AAAAAAAAAAAAAAAA</td>
<td>1.22288</td>
<td>1</td>
<td>None</td>
</tr>
<tr>
<th>1</th>
<td>HHHH</td>
<td>2.55566</td>
<td>NaN</td>
<td>{'idx_1': 'dark', 'idx_2': 'light'}</td>
</tr>
</tbody>
</table>
"""
每一個(gè) <td>
標(biāo)簽都代表一個(gè)單元格岛心,在 <td>
中的 td
后添加 bgcolor="#ffcc00"
,即可給
單元格賦予顏色篮灼。#ffcc00
表示黃色忘古。
我們可以寫個(gè)函數(shù)指定某個(gè)單元格賦予顏色。
import re
def Godsaycolor(df, html_text, idx_row, idx_col):
td_text = re.findall("<td>(.*?)</td>", html_text)
text = td_text[(idx_row) * df.shape[1] + idx_col]
html_text_fin = html_text.replace('<td>' + text,
'<td bgcolor="#ffcc00">' + text)
return html_text_fin
html_text9 = Godsaycolor(df_test, html_text8, 0, 3)
html_text10 = Godsaycolor(df_test, html_text9, 1, 0)
print(html_text10)
"""
<table border="1" class="dataframe"><caption>測(cè)試表格標(biāo)題</caption>
<thead>
<tr style="text-align: right;">
<th></th>
<th>col_a</th>
<th>col_b</th>
<th>col_c</th>
<th>col_d</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>AAAAAAAAAAAAAAAA</td>
<td>1.22288</td>
<td>1</td>
<td bgcolor="#ffcc00">None</td>
</tr>
<tr>
<th>1</th>
<td bgcolor="#ffcc00">HHHH</td>
<td>2.55566</td>
<td>NaN</td>
<td>{'idx_1': 'dark', 'idx_2': 'light'}</td>
</tr>
</tbody>
</table>
"""