question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

st.dataframe does not support multiindex dataframe styler formatting.

See original GitHub issue

I’m trying to apply a dictionary of formats via the pandas styler to a multiindex dataframe. But it throws and error.

image

You can see the error if you pip install yahooquery and streamlit run the below script

import streamlit as st

from yahooquery import Ticker

data = Ticker("ORSTED.CO").balance_sheet(frequency="annual")

st.header("Raw Data")
st.dataframe(data)

def get_default_format(data):
    format_dict = {}
    for column in data.columns:
        if data[column].dtype == "int64":
            format_dict[column] = "{0:,.0f}"
        elif data[column].dtype == "float64":
            format_dict[column] = "{0:,.2f}"
        else:
            format_dict[column] = "{0:}"
    return format_dict

st.header("Dataframe Index dropped and decimal symbol format applied")
data_reset_index = data.reset_index()
format_dict = get_default_format(data_reset_index)
st.dataframe(data_reset_index.style.format(format_dict))

st.header("Dataframe Index NOT dropped and decimal symbol format applied")
format_dict = get_default_format(data)
st.dataframe(data.style.format(format_dict))

and the error is

RuntimeError: Found unexpected row header "{'type': 'th', 'is_visible': True, 'value': 0, 'display_value': 0, 'id': 'level1_row0', 'class': 'row_heading level1 row0'}"
Traceback:
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\ScriptRunner.py", line 324, in _run_script
    exec(code, module.__dict__)
  File "C:\repos\private\awesomestreamlit\scripts\issues\issue_dataframe.py", line 28, in <module>
    st.dataframe(data.style.format(format_dict))
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\__init__.py", line 152, in wrapped_method
    return method.__get__(dg)(*args, **kwargs)
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\DeltaGenerator.py", line 88, in wrapped_method
    return method(self, *args, **kwargs)
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\DeltaGenerator.py", line 887, in dataframe
    set_data_frame, "dataframe", elementWidth=width, elementHeight=height
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\DeltaGenerator.py", line 392, in _enqueue_new_element_delta
    rv = marshall_element(msg.delta.new_element)
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\DeltaGenerator.py", line 884, in set_data_frame
    data_frame_proto.marshall_data_frame(data, delta.data_frame)
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\elements\data_frame_proto.py", line 55, in marshall_data_frame
    _marshall_styles(proto_df.style, df, styler)
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\elements\data_frame_proto.py", line 76, in _marshall_styles
    display_values = _get_custom_display_values(df, translated_style)
  File "c:\repos\private\awesomestreamlit\.venv\lib\site-packages\streamlit\elements\data_frame_proto.py", line 188, in _get_custom_display_values
    raise RuntimeError('Found unexpected row header "%s"' % cell)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

7reactions
Jesse-jAppscommented, Oct 2, 2020

Is there any progress concerning this so far?

5reactions
kantunicommented, Jan 5, 2021

This project has been stalled for a while, but I’ve resumed working on it recently.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas multiindex column styler - python - Stack Overflow
How do I write the styler so that all the nmem columns have thousand separators {:,} , 'pct_fem' are percentages to two decimal...
Read more >
Style Pandas Dataframe Like a Master - Towards Data Science
The basic idea behind styling is to leverage visual aids like color and format, in order to communicate insight more efficiently.
Read more >
Styling — pandas 0.25.1 documentation
This is a property that returns a Styler object, which has useful methods for formatting and displaying DataFrames. The styling is accomplished using...
Read more >
st.dataframe - Streamlit Docs
If 'data' is a pandas.Styler, it will be used to style its underlying DataFrame. Streamlit supports custom cell values and colors. (It does...
Read more >
Dataframe Styling using Pandas [Guided Walkthrough] | Tables
Learn how to use the pandas python library to style dataframes & add conditional formatting, bar charts, & more in this guided walkthrough.....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found