2017-03-15 28 views
2

我有一個熊貓據幀,我使用的是df.style對象,以便突出奇數行,所以:大熊貓DataFrame樣式化HTML顯示沒有索引?

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

然而,這總是打印出該指數。有沒有辦法告訴它不要這樣做?

回答

3

我翻翻了pandas.formats.style.Styler的源代碼,找不到一個超級簡單的方法來做到這一點。所以相反,這是一種怪異的方式。基本上,我告訴CSS表格不要顯示類row_heading和左上角的空框,其類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() 

結果:

enter image description here

+0

哇。這既可怕又美麗。 –