Pandas DataFrame styler HTML 显示没有索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42813123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:12:13  来源:igfitidea点击:

Pandas DataFrame styler HTML display without index?

htmlpandas

提问by Brian Postow

I have a pandas dataframe, I'm using the df.style object to make it highlight odd-numbered rows, so:

我有一个 Pandas 数据框,我使用 df.style 对象使其突出显示奇数行,因此:

def highlight_oddRow(s):
    return ['background-color: yellow' if s.name % 2 else '' for v in s]

table = pd.DataFrame(
    {'a': [3,9,8,0,2], 'b': [5,95, 9, 25,5], 'c': [23,54, 2, 3,5], 'row': [1, 2, 3, 4, 5]})

with open ('out.html','w') as out:
    print >> out, table.style.apply(highlight_oddRow, axis=1).render()

However, this always prints out the index. Is there a way to tell it not to do this?

但是,这总是打印出索引。有没有办法告诉它不要这样做?

回答by Tomas Farias

Since this is the first question that popped up when I searched for this issue on Stack Overflow, I thought it would be good to share a recent development: on Nov-17 a PRwas committed to the pandas repo that added the hide_indexmethod to stylerobjects.

由于这是我在 Stack Overflow 上搜索这个问题时出现的第一个问题,我认为分享最近的发展会很好:11 月 17 日,一个PR提交给了将hide_index方法添加到styler对象的 Pandas 存储库。

You can just call it before render:

你可以在渲染之前调用它:

def highlight_oddRow(s):
    return ['background-color: yellow' if s.name % 2 else '' for v in s]

table = pd.DataFrame(
    {'a': [3,9,8,0,2], 'b': [5,95, 9, 25,5], 'c': [23,54, 2, 3,5], 'row': [1, 2, 3, 4, 5]})

with open ('out.html','w') as out:
    print >> out, table.style.apply(highlight_oddRow, axis=1).hide_index().render()

Keep in mind the docs still claim these features are provisional and subject to change. More info here: https://pandas.pydata.org/pandas-docs/stable/style.html#Hiding-the-Index-or-Columns.

请记住,文档仍然声称这些功能是临时的,可能会发生变化。更多信息:https: //pandas.pydata.org/pandas-docs/stable/style.html#Hiding-the-Index-or-Columns

回答by Igor

I looked through the source code for pandas.formats.style.Styler, and couldn't find a super easy way to do it. So instead here is a hacky way. Basically I tell the CSS for the table to not display elements with class row_headingand the top left empty box, which has classes blank level0. Below is the code

我查看了 的源代码pandas.formats.style.Styler,但找不到超级简单的方法。所以这里是一种hacky方式。基本上我告诉表格的 CSS 不显示带有 class 的元素row_heading和左上角的空框,它有 classes blank level0。下面是代码

import pandas as pd

def highlight_oddRow(s):
    return ['background-color: yellow' if s.name % 2 else '' for v in s]

table = pd.DataFrame(
    {'a': [3,9,8,0,2], 'b': [5,95, 9, 25,5], 'c': [23,54, 2, 3,5], 'row': [1, 2, 3, 4, 5]})

with open ('out.html','w') as out:
    # Get the styler for the table
    styler = table.style

    # Set the display to none for row headings, and the blank box in the top left corner for the column headings
    styler.set_table_styles(
        [{'selector': '.row_heading',
          'props': [('display', 'none')]},
         {'selector': '.blank.level0',
          'props': [('display', 'none')]}])

    print >> out, styler.apply(highlight_oddRow, axis=1).render()

The result:

结果:

enter image description here

在此处输入图片说明

回答by Sambhav Kumar Dash

This can be done by using df.styler.hide_index().

这可以通过使用 df.styler.hide_index() 来完成。

print >> out, table.style.hide_index().apply(highlight_oddRow, axis=1).render()