html - 如何设置 Pandas Dataframe 标题的字体?

标签 html jupyter-notebook sklearn-pandas

我试图在 Jupyter 笔记本中并排显示两个表。
我有一些代码可以做到这一点:

header = ["Metric", "Test dataset"]

table1 = [["accuracy",               accuracy_test],
          ["precision",              precision_test],
          ["recall",                 recall_test],
          ["misclassification rate", misclassification_rate_test],
          ["F1",                     F1_test],
          ["r2",                     r2_test],
          ["AUC",                    auc_test],
          ["mse",                    mse_test],
          ["logloss",                logloss_test]
          ]

table2 = [['accuracy',               0.91943799],
          ['precision',              0.89705603],
          ['recall',                 0.94877461],
          ['misclassification rate', 0.08056201],
          ['F1',                     0.92219076],
          ['r2',                     0.67773888],
          ['AUC',                    0.91924997],
          ['mse',                    0.08056201],
          ['logloss',                2.78255718]]

def display_side_by_side(dfs:list, captions:list):
    output = ""
    combined = dict(zip(captions, dfs))
    styles = [
        dict(selector="caption", props=[("caption-side", "center"), ("font-size", "100%"), ("color", )])]
    for caption, df in combined.items():
        output += df.style.set_table_attributes("style='display:inline; font-size:110%' ").set_caption(caption)._repr_html_()
        output += "\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"
    display(HTML(output))

df1 = pd.DataFrame(table1, columns=header)
df2 = pd.DataFrame(table2, columns=header)

display_side_by_side([df1, df2], ['Current Performance', 'Previous Performance'])

我的 Jupyter 笔记本中的输出如下所示:

enter image description here

请注意,标题为浅色,并且它们不在表格上方居中。我怎样才能解决这个问题?

查尔斯

最佳答案

您已经定义了样式,但没有包含它们。语法也有点不正确。

styles = [dict(selector="caption", 
    props=[("text-align", "center"),
    ("font-size", "120%"),
    ("color", 'black')])]    # the color value can not be None
# ...

    output += df.style.set_table_attributes("style='display:inline; font-size:110%' ")
        .set_caption(caption)
        .set_table_styles(styles)    # include styles
        ._repr_html_()

关于html - 如何设置 Pandas Dataframe 标题的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58864672/

相关文章:

python-3.x - AttributeError: 模块 'sklearn.datasets' 没有属性 'load_titanic'

html - 使用 jQuery 更改链接点击上的 Woocommerce 产品数量

php - Post 函数无法按预期与数据库一起运行

javascript - 如何在不使用 Select ID 的情况下创建基于下拉选择的 jQuery URL 重定向?

visual-studio-code - PyPlot 图未在 VS Code Jupyter 中显示 "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."

python - 将 Pandas 数据框列建模为类别列表

javascript - 第二种形式未获取数据

bash - 从安装在 Windows 10 上的 ubuntu bash 启动 jupyter notebook

python - 在 python 3 中水平而不是垂直显示列表结果

使用隐式反馈方法将 python 字典转换为矩阵