How to hide dataframe index on streamlit?
Asked Answered
R

3

8

I want to use some pandas style resources and I want to hide table indexes on streamlit.

I tryed this:

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

but regardless the .hide_index() I got this:

enter image description here

Ideas to solve this?

Russom answered 7/11, 2021 at 19:11 Comment(5)
better create minimal working code which we could simply copy and run.Cultivable
Can't you table1.set_index('N') than create st.dataframe(table1) ?Sext
Ok, @furas, I've edited the post so that you can run the code. I'm using pandas 1.3.4 and some features might be recent.Russom
@Jamjitul, this works, but the column "N" would have no label. It would be important that the table is properly formatted, with proper headings etc.Russom
documentation for st.dataframe shows "Styler support is experimental!" and maybe this can be the problem.Cultivable
C
5

Documentation for st.dataframe shows "Styler support is experimental!"
and maybe this is the problem.

But I can get table without index if I use .to_html() and st.write()

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)

enter image description here

Cultivable answered 8/11, 2021 at 12:51 Comment(0)
S
4

Try using the below code:

st.dataframe(data,hide_index=True)

st.dataframe function has an argument that is by default None, change it to True and it might work

Sussman answered 12/10, 2023 at 23:20 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Fated
Please validate your response using provided input, as done in accepted answer.Bullock
G
2

Another option is using a CSS selector to remove the index column. Like explained in the docs, you can do the following with st.table:

# import packages
import streamlit as st
import pandas as pd

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

# CSS to inject contained in a string
hide_table_row_index = """
            <style>
            thead tr th:first-child {display:none}
            tbody th {display:none}
            </style>
            """

# Inject CSS with Markdown
st.markdown(hide_table_row_index, unsafe_allow_html=True)

# Display a static table
st.table(table1.style.format(subset=['mean'],
             decimal=',', precision=2).bar(subset=['mean'], align="mid"))

Output:

enter image description here

As you can see the index is gone. Keep in mind that the table function takes the full page.

Gennagennaro answered 16/9, 2022 at 16:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.