Skip to content

Display multiindex dataframe as “sparsified” table #5370

@sfc-gh-kbregula

Description

@sfc-gh-kbregula

Problem

I'm always frustrated when I am trying to display the multiindex dataframe, but it is not readable

Solution

pandas when displaying a dataframe that have multiindex skips duplicate values to make the console output a bit easier on the eyes.

>>> import pandas as pd
>>> import numpy as np
>>>
>>> tuples = list(
...     zip(
...         *[
...             ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
...             ["one", "two", "one", "two", "one", "two", "one", "two"],
...         ]
...     )
... )
>>> index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
>>> df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=["A", "B"])
>>> df
                     A         B
first second
bar   one    -0.883183  0.480221
      two     0.861466 -0.659678
baz   one    -0.039308  0.116815
      two     0.139792  0.787177
foo   one     1.638924 -0.135970
      two     0.408906  0.394975
qux   one    -0.467613 -0.239692
      two     1.315613  0.867033

I think it's worth the st.table command to work in a similar way. Currently the table is displayed as below:
Screenshot 2022-09-19 at 12 29 18

As a workaround, we can render the table to HTML via pandas, but that's not perfect.
Screenshot 2022-09-19 at 12 30 42

This is especially valuable for pivot tables.

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(
...     {
...         "A": ["one", "one", "two", "three"] * 3,
...         "B": ["A", "B", "C"] * 4,
...         "C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 2,
...         "D": np.random.randn(12),
...         "E": np.random.randn(12),
...     }
... )
>>> pd.pivot_table(df, values="D", index=["A", "B"], columns=["C"])
C             bar       foo
A     B
one   A  0.590631  0.536188
      B  0.933343 -0.708869
      C  0.521529  0.377555
three A -0.665092       NaN
      B       NaN  1.060647
      C -1.138812       NaN
two   A       NaN  0.500034
      B  0.074022       NaN
      C       NaN  0.810322

Additional context

MultiIndex / advanced indexing in pandas documentation


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you'd like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

Views

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:st.dataframeRelated to the `st.dataframe` elementfeature:st.tableRelated to the `st.table` elementtype:enhancementRequests for feature enhancements or new featuresupstreamIssue caused by upstream dependency

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions