【问题标题】:Pandas Styler Hiding IndexPandas Styler 隐藏索引
【发布时间】:2021-09-09 11:37:40
【问题描述】:

我基本上想要一个具有右对齐表格单元格条目和最小列宽 100 的表格。我有两种方法可以在 HTML 中显示 Pandas 数据框df

html = df.style.applymap(color_negative_red).set_precision(5).set_table_attributes(
    'class = "dataframe table-bordered table-striped table-hover').set_properties(
        **{'width': '10em', 'text-align': 'right'}).render()

display(HTML(html)

在这种方法中,我不知道如何使用 Styler 对象隐藏行索引(即,在渲染之前我无法设置 index = False 参数。

和:

return df.to_html(index=ind, justify = {"right"}, col_space = 100,
                  classes = ["table-bordered", "table-striped", "table-hover"])

此方法使用to_html,似乎不适用于justifycol_space 参数。

我该如何做/找到解决这两个问题的方法?

【问题讨论】:

    标签: python pandas dataframe pandas-styles


    【解决方案1】:

    我认为hide_index() 可能有助于在您的第一种方法中删除索引。来自docs

    可以通过调用Styler.hide_index 隐藏索引。可以通过调用Styler.hide_columns 并传入列名或列的切片来隐藏列,使其无法呈现。

    所以以下应该可以工作,

    ....
    .hide_index().render()
    

    【讨论】:

    • 虽然这可能会回答问题,但如果您可以链接到例如一些文档,或解释为什么会这样。
    • pandas.pydata.org/pandas-docs/stable/style.html, "可以通过调用 Styler.hide_index 隐藏索引。通过调用 Styler.hide_columns 并传入列名或列。”直到本周我自己才意识到这一点,看起来像 OP,我一直在谷歌上搜索同样的问题。
    猜你喜欢
    • 2017-08-06
    • 2017-12-03
    • 2022-11-12
    • 2021-04-26
    • 2021-05-14
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多