Hide row indices when displaying a dataframe in Streamlit v1.16.0

How do I hide the indices in a dataframe in Streamlit v1.16.0?

A previous post here showed how to achieve this in an earlier version of Streamlit: Hide row indices when displaying a dataframe - Streamlit Docs

I can’t seem to find a solution here, and I would have thought this would have been a pretty standard activity conducted on dataframes

I just tested and it seems that Streamlit’s handling and a DataFrame Styler is not correclty handling df.style.hide(axis="index"). I would think that would be the best way to do it but alas:

import streamlit as st
import pandas as pd

df = pd.DataFrame({'A':[1,2,3],'B':['a','b','c']})
st.dataframe(df)
st.dataframe(df.style.hide(axis="index"))
st.markdown(df.style.hide(axis="index").to_html(), unsafe_allow_html=True)

image

That being the case, the Streamlit Documentation does tell you exactly the CSS trickery to accomplish this:

Edit: I see a note that that doesn’t work past version 1.10, and that I also skipped over where you already linked to it. Oops. The modern implementation is a lot less receptive to CSS hacks, but I’m fiddling.

Yeah, I think it’s going to be hard to accomplish it through the st.dataframe method. The interactivity makes it much more complicated. It’s easy enough to accomplish with a static table as shown above, but interactive options haven’t come out pretty for me.

I tried converting to a plotly go.Table, but that only allows dragging and dropping columns without sorting.

I tried itables, and that does work, but it needs a lot of css styling to make it pretty.

import itables

df=pd.DataFrame()

st.components.v1.html(itables.to_html_datatable(df), height=400)

Sorry I don’t have anything slick, but I think it’s worth raising a bug on GitHub if it’s not there already being that officially Streamlit is supposed to respect a pandas styler.

Here’s a GitHub issue you can vote on:

Thanks for your efforts. It is a little frustrating as this is usually a standard feature of these types of controls/ objects from my experience. The Streamlit app is only a temp solution until a more appropriate solution can be developed in one of our business systems. I will likely vote on the GitHub issue.

Thanks again for your assistance

1 Like

Hey @Nathan_Johnston,
I will admit I am quite new to python and streamlit, so apologies if this is not an ideal solution, but I got around this by setting my first column as the index instead.

Using @mathcatsand example, see second df below:

import streamlit as st
import pandas as pd

df = pd.DataFrame({'A':[1,2,3],'B':['a','b','c']})
st.dataframe(df)
st.dataframe(df.set_index(df.columns[0]))

image

3 Likes

@jmilno this works as expected for me. would be great if this behavior was controlled by a feature of st.dataframe (like st.dataframe(hide_index=True) .

One note, doing this for some reason doesn’t work but your method does.

df.set_index(df.columns[0])
st.dataframe(df) 
#still shows index

I’ll note that ideally this should work both in rendering the dataframe in Streamlit but also downstream to exporting the file via

df.to_csv().encode('utf-8')

1 Like

Very nice solution, thanks!

1 Like

We released a lot of new configuration options for st.dataframe and st.data_editor in 1.23. This also includes the hide_index parameter that lets you easily hide the index:

import streamlit as st
import pandas as pd

df = pd.DataFrame({'A':[1,2,3],'B':['a','b','c']})
st.dataframe(df, hide_index=True)

6 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.