如何使用 .style 为 headers 列添加边框并为 pandas 数据框添加标题?

How to add a border to column headers and caption to pandas dataframe using .style?

我正在尝试为列 headers 添加边框,并为下面的数据框添加标题。我可以成功地为标题添加边框,但是当我尝试在列 headers 周围添加边框时,我相信它会覆盖标题边框和格式。有没有办法为两者添加边框?我尝试添加两个 .set_table_styles() 语句,我也尝试将它们合并到同一块代码中,但我一定误解了 运行.

时如何解释代码
final = unique_shows_imdb_final.style.bar(subset=["Netflix IMDb Score"], color='')\
                     .bar(subset=["Hulu IMDb Score"], color='')\
                     .bar(subset=["Prime IMDb Score"], color='')\
                     .bar(subset=["Disney+ IMDb Score"],color='').background_gradient(cmap=sns.cm.rocket_r,axis=None).hide_index().format(precision=1)

final.set_properties(subset=['Rank'], **{'font-weight': 'bold'})



final.set_properties(
    **{'border': '1px black solid !important'}
).set_table_styles([{
    'selector': '',
    'props': [('border', '2px black solid !important')]}]
).set_caption("Top 20 TV Shows per Streaming Platform").set_table_styles([{
    'selector': 'caption',
    'props': [
        ('color', 'black'),
        ('font-size', '25px'),
        ('text-align','center'),
        ('border', '3px black solid !important')
    ]
}])

final = unique_shows_imdb_final.style.bar(subset=["Netflix IMDb Score"], color='')
.bar(subset=["Hulu IMDb Score"], color='')
.bar(subset=["Prime IMDb Score"], color='')
.bar(subset=["Disney+ IMDb Score"],color='').background_gradient(cmap=sns.cm.rocket_r,axis=None).hide_index()。格式(精度=1)

final.set_properties(subset=['Rank'], **{'font-weight': 'bold'})



final.set_properties(
    **{'border': '1px black solid !important'}
).set_table_styles([{
    'selector': '',
    'props': [('border', '2px black solid !important')]}]
).set_caption("Top 20 TV Shows per Streaming Platform").set_table_styles([{
    'selector': 'caption',
    'props': [
        ('color', 'black'),
        ('font-size', '25px'),
        ('text-align','center'),
        ('border', '3px black solid !important')
    ]
}])

final.set_table_styles([{'selector': 'th', 'props': [('font-size', '10pt'),('border-style','solid'),('border-width','1px')]}])

set_table_styles 默认有 overwrite=True.

来自文档:

覆盖bool,默认True

Styles are replaced if True, or extended if False. CSS rules are preserved so most recent styles set will dominate if selectors intersect. New in version 1.2.0.

这意味着(默认情况下)每次调用都会覆盖以前的样式,我们可以设置 overwrite=False 并添加 table 样式中缺少的“border-color”:

final.set_properties(
    **{'border': '1px black solid !important'}
).set_table_styles([{
    'selector': '',
    'props': [('border', '2px black solid !important')]}]
).set_caption("Top 20 TV Shows per Streaming Platform").set_table_styles([{
    'selector': 'caption',
    'props': [
        ('color', 'black'),
        ('font-size', '25px'),
        ('text-align', 'center'),
        ('border', '3px black solid !important')
    ]
}], overwrite=False)  # Don't overwrite previous styles

final.set_table_styles([{
    'selector': 'th', 'props': [
        ('font-size', '10pt'),
        ('border-style', 'solid'),
        ('border-width', '1px'),
        ('border-color', 'black') # Missing Border Color here
    ]
}], overwrite=False)  # Don't overwrite previous styles

示例数据:

import numpy as np
import pandas as pd

np.random.seed(5)
df = pd.DataFrame(np.random.randint(1, 100, (4, 6)))
final = df.style

示例输出:


但是,通常最好将 table 样式设置为最后并传递一个选择器字典列表:

final.set_properties(
    **{'border': '1px black solid !important'}
).set_caption("Top 20 TV Shows per Streaming Platform").set_table_styles([
    # Selector 1
    {'selector': '',
     'props': [('border', '2px black solid !important')]},
    # Selector 2
    {'selector': 'caption',
     'props': [
         ('color', 'black'),
         ('font-size', '25px'),
         ('text-align', 'center'),
         ('border', '3px black solid !important')]},
    # Selector 3
    {'selector': 'th', 'props': [
        ('font-size', '10pt'),
        ('border-style', 'solid'),
        ('border-width', '1px'),
        ('border-color', 'black')  # Missing Border Color here
    ]}
])

这会生成与上述样式完全相同的框架。