2016-11-21 82 views
2

如果我使用DataFrame.set_index,我得到這樣的結果:在HTML顯示大熊貓數據框沒有額外的行

import pandas as pd 

df = pd.DataFrame([['foo',1,3.0],['bar',2,2.9], 
        ['baz',4,2.85],['quux',3,2.82]], 
       columns=['name','order','gpa']) 
df.set_index('name') 

enter image description here

注意不必要的行...我知道它這樣做,因爲它保留列標題的左上角單元格,但我不關心它,如果我在演示文稿中使用它,它會使我的表格看起來有些不專業。

如果我不使用DataFrame.set_index,額外的排走了,但我得到的數字排索引,這是我不想:

enter image description here

如果我使用to_html(index=False)然後我解決這些問題,但第一列是不加粗:

import pandas as pd 
from IPython.display import HTML 

df = pd.DataFrame([['foo',1,3.0],['bar',2,2.9], 
        ['baz',4,2.85],['quux',3,2.82]], 
       columns=['name','order','gpa']) 
HTML(df.to_html(index=False)) 

enter image description here

如果我想控制造型,使名稱粗體,我想我可以通過HTML(df.style.do_something_here().render())使用新的Styler API,但我不知道如何實現index=False功能。

什麼是黑客要做的? (除了自己構建HTML)

回答

3

我在source for Styler中探出頭來,想通了;如果設置df.index.names = [None]那麼這抑制了「額外」行(與列標題,我真的不關心沿):

import pandas as pd 

df = pd.DataFrame([['foo',1,3.0],['bar',2,2.9], 
        ['baz',4,2.85],['quux',3,2.82]], 
       columns=['name','order','gpa']) 
df = df.set_index('name') 
df.index.names = [None] 
df 

enter image description here

+0

不能確定這是什麼一樣,但是。 :/ –