Using Pandas Styler with column_config

Hi all - ran into my first Streamlit issue I haven’t been able to resolve from the forums.

Summary

Using column_config in conjunction with a Pandas styler object doesn’t appear to work.

Steps to reproduce

According to the docs, I can pass a pandas styler object to
st.dataframe. This works.

df = pd.DataFrame({
    'cost': [25.99, 97.45, 64.32, 14.78],
    'grams': [101.89, 20.924, 50.12, 40.015]
    })

st.write(df)

st.dataframe(df.style.highlight_max(axis=0))

However, passing a styled df to st.dataframe appears to negate column_config:

st.dataframe(df.style.highlight_max(axis=0), 
             column_config={'cost': st.column_config.NumberColumn('Cost',format="$%.2g"),
                            'grams': st.column_config.NumberColumn('Grams',format="%.2f")})

image

Expected behavior:

The st.dataframe returns a styled dataframe with highlighting, and column_config formatting.

Am I doing it wrong? Or is it not possible to apply column_config to a styler object? If the latter, is the answer to do all the formatting in Pandas ahead of time?

Thanks for the input!

Debug info

  • Streamlit version: 1.27
  • Python version: 3.11.5, with Conda

Thanks for your question, @lawtj! :pray:

@lukasmasuch, can you please confirm whether this is indeed a known limitation for column_config?

Thanks,
Charly

Unfortunately, applying formatting via colum_config on a styled dataframe is currently not compatible :frowning: You can find some more info and a workaround in my answer on this issue: Number formatting in NumberColumn does not work with a styled data frame · Issue #7329 · streamlit/streamlit · GitHub

1 Like

Thanks for confirming, @lukasmasuch!