【问题标题】:How to hide dataframe index on streamlit?如何在streamlit上隐藏数据帧索引?
【发布时间】:2021-12-20 20:04:11
【问题描述】:

我想使用一些 pandas 风格的资源,我想在 streamlit 上隐藏表索引。

我试过了:

import streamlit as st
import pandas as pd


table1 = pd.DataFrame({'N':[10, 20, 30], 'mean':[4.1, 5.6, 6.3]})
st.dataframe(table1.style.hide_index().format(subset=['mean'],
             decimal=',', precision=2).bar(subset=['mean'], align="mid"))

但不管.hide_index() 我得到了这个:

解决这个问题的想法?

【问题讨论】:

  • 更好地创建最小的工作代码,我们可以简单地复制和运行。
  • 你不能table1.set_index('N') 创建st.dataframe(table1) 吗?
  • 好的,@furas,我已经编辑了帖子,以便您可以运行代码。我使用的是 pandas 1.3.4,有些功能可能是最新的。
  • @Jamjitul,这行得通,但“N”列没有标签。表格格式正确、标题正确等很重要。
  • st.dataframe 的文档显示 "Styler support is experimental!",这可能是问题所在。

标签: python pandas streamlit pandas-styles


【解决方案1】:

st.dataframe 的文档显示 "Styler support is experimental!"
也许这就是问题所在。

但是如果我使用.to_html()st.write(),我可以在没有index 的情况下获得表格

import streamlit as st
import pandas as pd

df = pd.DataFrame({'N':[10, 20, 30], 'mean':[4.1, 5.6, 6.3]})

styler = df.style.hide_index().format(subset=['mean'], decimal=',', precision=2).bar(subset=['mean'], align="mid")

st.write(styler.to_html(), unsafe_allow_html=True)

#st.write(df.to_html(index=False), unsafe_allow_html=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 2021-06-28
    • 2014-02-10
    • 1970-01-01
    • 2018-02-26
    相关资源
    最近更新 更多