Search code examples
pythonpandasdataframestreamlitpandas-styles

How to use pandas.style with streamlit


I am creating an app with Streamlit and I wish to print out a pandas dataframe in which I can change the background color of each cell of the dataframe. I found out that this is supported by pandas with the package 'pandas.style' but I couldn't make it work with streamlit. I was thus wondering if this is possible with streamlit or if anyone has found a work around.

Here is what I have tried:

df.style.apply(lambda x: "background-color: red")
st.dataframe(df)

Thank you in advance for your help!


Solution

  • Calling df.style.apply or df.style.applymap returns a styled dataframe, while you seem to assume that it operates in-place.

    Change your code to st.dataframe(df.style.apply(lambda x: "background-color: red")) and it should work.