在HTML中显示Pandas DataFrame,不显示多余的行 [英] displaying Pandas DataFrame in HTML without the extra row

查看:353
本文介绍了在HTML中显示Pandas DataFrame,不显示多余的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用DataFrame.set_index,则会得到以下结果:

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],
                   ['baz',4,2.85],['quux',3,2.82]],
                 columns=['name','order','gpa'])
df.set_index('name')

请注意不必要的行...我知道这样做是因为它为列标题保留了左上角的单元格,但是我不在乎,如果我在表格中使用它,它会使我的表显得有些不专业.演示文稿.

Note the unnecessary row... I know it does this because it reserves the upper left cell for the column title, but I don't care about it, and it makes my table look somewhat unprofessional if I use it in a presentation.

如果我不使用DataFrame.set_index,多余的行就消失了,但是我得到了数字行索引,这是我不想要的:

If I don't use DataFrame.set_index, the extra row is gone, but I get numeric row indices, which I don't want:

如果使用to_html(index=False),则可以解决这些问题,但是第一列不是粗体:

If I use to_html(index=False) then I solve those problems, but the first column isn't bold:

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

如果我想控制样式使名称变为黑体字,我想我可以使用新的HTML(df.style.do_something_here().render()) ="nofollow noreferrer"> Styler API ,但是我不知道如何实现index=False功能.

If I want to control styling to make the names boldface, I guess I could use the new Styler API via HTML(df.style.do_something_here().render()) but I can't figure out how to achieve the index=False functionality.

黑客要做什么? (除了自己构造HTML)

What's a hacker to do? (besides construct the HTML myself)

推荐答案

我在

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

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

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