Styling Streamlit's Dataframe Index & Header

Hey! I love streamlitā€™s built-in dataframe for many obvious reasons:

  • Responsive for desktop & mobile
  • You can always see column names & index names even when scrolling around
  • Allows me to display my fancy dataframes with minimal headache

Background: I work in an industry thatā€™s super opinionated on formatting its tables. Using pandas built-in styling, Iā€™m able to achieve 99% of my needs (highlighting subtotals, formatting negatives with parentheses, all that jazz). However, all this pandas styling only applies to the values of the dataframe and not the index/column headers.

Request: Has anyone figured out a way to style the index & column header as well? Perhaps the below example highlights my issue. Iā€™d like to make ā€œTotal UCFā€ dark grey as Iā€™ve already done with pandas styling. And Iā€™d also like to make the headers ā€œEntry, 2018, 2019,ā€ etc. dark blue & bold with white font. Any ideas?! (actual figures hidden b/c confidential)

I can (& have) achieved this with plotly tables. But theyā€™re a little less ideal because theyā€™re effectively static images without the magic of keeping the header & index in-place as the user scrolls around the dataframe. Any clue how to do this with the native st.dataframe()? Or something that behaves similarly (i.e. can do the three things bulleted above). Iā€™m open to hacks and other libraries you may know, but the ideal would be Streamlit-native (but beggars canā€™t be choosers).

2 Likes

I have the same issue. I would especially like to format the table header. I remember reading a post saying this was not possible. Any positive update would be most welcome. :slight_smile:

2 Likes

I tried the code shown here: Table Visualization ā€” pandas 1.3.4 documentation (pydata.org) - No joy.

styles = [
    {'selector': 'th', 'props': 'font-size: 10px; font-weight: bold;'},
    ]

_pra_show_data_styled = _pra_show_data_styled.set_properties(**{'font-size': '10pt'})
_pra_show_data_styled = _pra_show_data_styled.set_table_styles(styles, overwrite=False)

The cell formatting works - just not he header style.

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