如何更改 pandas DataFrame标头的样式? [英] How can I change the styles of pandas DataFrame headers?

查看:78
本文介绍了如何更改 pandas DataFrame标头的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下生成HTML表的Python脚本.现在,我想更改表格的某些样式,但是在更改标题的字体大小时遇到​​问题.

I have the following Python script which generates a HTML table. Now I want to change some styles of the table, but I'm having problems with changing the font sizes of the headers.

import pandas as pd

df = pd.DataFrame({'col1': [1, 2, 3, 4]})

html = (df.style
        .set_table_styles({'selector': 'th', 'props': [('font-size', '5pt')]})
        .set_properties(**{'font-size': '10pt'}).render())

f = open('test.html', 'wb')
f.write(html)

如何更改标题的样式?

<style  type="text/css" >
    #T_f85befdc_2093_11e8_85e2_08002718d81b  {
    }    #T_f85befdc_2093_11e8_85e2_08002718d81b  {
    }    #T_f85befdc_2093_11e8_85e2_08002718d81brow0_col0 {
            font-size:  10pt;
        }    #T_f85befdc_2093_11e8_85e2_08002718d81brow1_col0 {
            font-size:  10pt;
        }    #T_f85befdc_2093_11e8_85e2_08002718d81brow2_col0 {
            font-size:  10pt;
        }    #T_f85befdc_2093_11e8_85e2_08002718d81brow3_col0 {
            font-size:  10pt;
        }</style>  
<table id="T_f85befdc_2093_11e8_85e2_08002718d81b" > 
<thead>    <tr> 
        <th class="blank level0" ></th> 
        <th class="col_heading level0 col0" >col1</th> 
    </tr></thead> 
<tbody>    <tr> 
        <th id="T_f85befdc_2093_11e8_85e2_08002718d81b" class="row_heading level0 row0" >0</th> 
        <td id="T_f85befdc_2093_11e8_85e2_08002718d81brow0_col0" class="data row0 col0" >1</td> 
    </tr>    <tr> 
        <th id="T_f85befdc_2093_11e8_85e2_08002718d81b" class="row_heading level0 row1" >1</th> 
        <td id="T_f85befdc_2093_11e8_85e2_08002718d81brow1_col0" class="data row1 col0" >2</td> 
    </tr>    <tr> 
        <th id="T_f85befdc_2093_11e8_85e2_08002718d81b" class="row_heading level0 row2" >2</th> 
        <td id="T_f85befdc_2093_11e8_85e2_08002718d81brow2_col0" class="data row2 col0" >3</td> 
    </tr>    <tr> 
        <th id="T_f85befdc_2093_11e8_85e2_08002718d81b" class="row_heading level0 row3" >3</th> 
        <td id="T_f85befdc_2093_11e8_85e2_08002718d81brow3_col0" class="data row3 col0" >4</td> 
    </tr></tbody> 
</table> 

推荐答案

我发现我需要将参数作为列表传递给Styler.set_table_styles().现在,它可以使用以下代码.

I've found out I need to pass a parameter as a list to Styler.set_table_styles(). It's now working with the following code.

html = (df.style
        .set_table_styles([{'selector': 'th', 'props': [('font-size', '5pt')]}])
        .set_properties(**{'font-size': '10pt'}).render())

这篇关于如何更改 pandas DataFrame标头的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆