Pandas Dataframe 隐藏索引功能?

v6ylcynt  于 2023-01-07  发布在  其他
关注(0)|答案(8)|浏览(265)

有没有可能在显示PandasDataFrame时隐藏索引,以便只有列名显示在表的顶部?
这将需要对ipython notebook中的html表示和to_latex()函数(我在nbconvert中使用)都有效。

kmbjn2e3

kmbjn2e31#

正如@waitingkuo所指出的,index=False是你所需要的,如果你想在你的ipython笔记本中保持漂亮的表格布局,你可以用途:

from IPython.display import display, HTML
display(HTML(df.to_html(index=False)))
mklgxw1f

mklgxw1f2#

从v. 0.17.1开始,可以通过styling隐藏索引,请参见隐藏索引或列:如果df是您的数据框,则只需

df.style.hide_index()

请注意,样式只在笔记本电脑中有效,在LaTeX转换中无效。

py49o6xq

py49o6xq3#

设置index=False
对于ipython笔记本:

print df.to_string(index=False)

对于乳胶:

df.to_latex(index=False)
pqwbnv8z

pqwbnv8z4#

我把下面的单元格添加到我的笔记本中,它在Jupyter 4.0.2中运行良好。
注:即使没有索引,它也会删除“any”表的第一列。

# Execute this cell to remove the first column of dataframe tables (to remove index column)
from IPython.core.display import HTML
HTML("""
<style>
    table.dataframe thead th:first-child {
        display: none;
    }
    table.dataframe tbody th {
        display: none;
    }
</style>
""")
w8biq8rn

w8biq8rn5#

你试试这个代码,可能会有帮助。

dataframe.style.hide_index()
66bbxpm5

66bbxpm56#

设置index=False
例如:

DataFrame.to_csv("filename", index=False)

这会有用的。

nimxete2

nimxete27#

样式.hide_index自Pandas 1.4起已不再使用。对于那些想知道如何在带有最新Pandas的笔记本中隐藏索引的用户,请使用用途:

df.style.hide(axis='index')
f0brbegy

f0brbegy8#

试试看

df.style.hide(axis="index")

否则,您将看到:

FutureWarning: this method is deprecated in favour of `Styler.hide(axis="index")`
  df.loc[df.index,['name','depth']].style.hide_index()

参见DEPR REF: hide(axis=..) replaces hide_index and hide_columns #43771

相关问题