Pandas DataFrame Styler HTML显示没有索引? [英] Pandas DataFrame styler HTML display without index?

查看:132
本文介绍了Pandas DataFrame Styler HTML显示没有索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,我正在使用df.style对象使其突出显示奇数行,所以:

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

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?

推荐答案

我浏览了pandas.formats.style.Styler的源代码,但找不到超级简单的方法.因此,这是一种骇人听闻的方法.基本上,我告诉表的CSS不显示类别为row_heading的元素,而左上角的空白框为类别为blank level0的空白框.下面是代码

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_heading and the top left empty box, which has classes blank level0. Below is the code

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()

结果:

这篇关于Pandas DataFrame Styler HTML显示没有索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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