displaying Pandas DataFrame in HTML without the extra row

后端 未结 2 1996
后悔当初
后悔当初 2021-01-13 21:13

If I use DataFrame.set_index, I get this result:

import pandas as pd

df = pd.DataFrame([[\'foo\',1,3.0],[\'bar\',2,2.9],
                   [\'         


        
相关标签:
2条回答
  • 2021-01-13 21:38

    I poked around in the source for Styler and figured it out; if you set df.index.names = [None] then this suppresses the "extra" row (along with the column header that I don't really care about):

    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
    

    0 讨论(0)
  • 2021-01-13 21:40

    These days pandas actually has a keyword for this:

    df.to_html(index_names=False)
    

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html

    0 讨论(0)
提交回复
热议问题