简体   繁体   中英

Plotly Table Multiindex Dataframe

Is there a way to create a Plotly Table from a multiindex df which preserves the merged cell labels: 在此处输入图像描述

Here is the code, note the numbers should be under the two columns on the right:

table = go.Table(
    header=dict(values=headers),
    cells=dict(values=df.T.values))

fig = go.Figure(data=table).update_layout()
fig.show()

Result:

I think the easiest way to accomplish what you want is to reset the index of your DataFrame so that the NAME and CATEGORY columns are no longer in the index, then replace duplicate values in the NAME column with a blank string '' . Then your DataFrame will look like this (with placeholder values under SALES ):

               NAME     CATEGORY  SALES DISC
0         Furniture       Tables      0  25%
1                      Bookcases      1  22%
2                         Chairs      2  17%
3                    Furnishings      3  15%
4   Office Supplies    Fasteners      4   9%
5                            Art      5   8%
6                      Envelopes      6   8%
7                          Paper      7   7%
8                        Storage      8   7%
9                       Supplies      9   7%
10                        Labels     10   6%
11                       Binders     11  39%
12                    Appliances     12  15%

This DataFrame is easier to pass to go.Table as you don't have to worry about the multiindex, yet it is formatted the same as the multiindex DataFrame.

df_table = df.reset_index()
df_table.loc[df_table['NAME'].duplicated(), 'NAME'] = ''

table = go.Table(
    header=dict(values=df_table.columns.tolist()),
    cells=dict(values=df_table.T.values)
    )

fig = go.Figure(data=table).update_layout()
fig.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM