pythonpandasstreamlitpandas-styles

How to hide dataframe index on streamlit?


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?


Solution

  • Documentation (version 1.22.0) 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


    EDIT 2025:

    In original code problem is that new version doesn't allow to use df.style.hide_index()
    but it needs df.style.hide()

    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().format(subset=['mean'], decimal=',', precision=2).bar(subset=['mean'], align="mid")
    
    st.write(styler.to_html(), unsafe_allow_html=True)
    

    And finally it shows text in the middle

    enter image description here


    It seems newer versions (since 1.23.0) have option hide_index in st.dataframe() to remove index but when I use it with styler then it removes color bars from table.

    I can simulate bars in st.dataframe with column_config and ProgressColumn
    (and it gives the same result with df or styler - so I don't need styler)

    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().format(subset=['mean'], decimal=',', precision=2).bar(subset=['mean'], align="mid")
    
    st.dataframe(
        df,
        #styler,  # use instead of `df`
        hide_index=True,
        column_config = {
            "mean": st.column_config.ProgressColumn(
                format="%.2f",
                #min_value=0,
                max_value=6.3,
            )
        }
    )
    

    enter image description here


    You can also use None in column_config to remove some column.
    For index you can use string "_index"

    st.dataframe(
        df,
        #styler,
        #hide_index=True,
        column_config = {
            "mean": st.column_config.ProgressColumn(
                format="%.2f",
                #min_value=0,
                max_value=6.3,
            ),
            '_index': None,  # hide index
            #'N': None       # hide column `N`
        }
    )
    

    Full code used for tests (2025):

    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().format(subset=['mean'], decimal=',', precision=2).bar(subset=['mean'], align="mid")
    
    st.write("DataFrame - df or styler - hide_index")
    st.dataframe(
        df,
        #styler,
        hide_index=True,
    )
    
    st.write("DataFrame - df or styler - ProgressColumn")
    st.dataframe(
        df,
        #styler,
        hide_index=True,
        column_config = {
            "mean": st.column_config.ProgressColumn(
                format="%.2f",
                #min_value=0,
                max_value=6.3,
            ),
            #'_index': None,  # hide index
            #'N': None,       # hide column `N`
        }
    
    )
    
    st.write("Table - styler")
    st.table(
        styler.set_properties(
        subset=['mean'],  # Only target column `mean`
        **{'text-align': 'center'}   # doesn't work
        ),
    )
    
    st.write("write - styler - to_html")
    st.write(styler.to_html(), unsafe_allow_html=True)
    
    #st.write(df.to_html(index=False), unsafe_allow_html=True)
    

    enter image description here