我有10-11列的熊猫数据帧。我想使用pd.to_html(index=False,border=0)将这个数据帧转换成html。但是,我想要更改返回表的CSS。我们可以改变数据帧本身的列宽,或者格式化pd.to_html()返回的表的标签吗?
预期表格的Html代码
<table>
<thead>
<th width="5%">A</th>
<th>B</th>
<th>C</th>
<th width="3%">D</th>
<th>E</th>
<th>F</th>
</thead>
<tbody>
</tbody>
</table>目前在pd.to_html()之后,我得到了这个表
<table border="0" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
</tr>
</thead>
<tbody>
</tbody>
</table>如果tr标记将在as标记之后从输出中删除,那就更好了,因为它不是必需的
发布于 2021-08-14 12:40:23
您可以使用df的样式:
df_html = df.style.set_table_styles({
'A': [{'selector': 'thead tr',
'props': [('width', '5%')]}],
'D': [{'selector': 'thead tr',
'props': [('width', '3%')]}],
})
print(df_html.render(sparse_index=True))输出非常冗长:
<style type="text/css">
#T_0167d_ thead tr.col0 {
width: 5%;
}
#T_0167d_ thead tr.col3 {
width: 3%;
}
</style>
<table id="T_0167d_">
<thead>
<tr>
<th class="blank level0" > </th>
<th class="col_heading level0 col0" >A</th>
<th class="col_heading level0 col1" >B</th>
<th class="col_heading level0 col2" >C</th>
<th class="col_heading level0 col3" >D</th>
<th class="col_heading level0 col4" >E</th>
<th class="col_heading level0 col5" >F</th>
</tr>
</thead>
<tbody>
</tbody>
</table>https://stackoverflow.com/questions/68782958
复制相似问题