Reputation: 1043
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()
Upvotes: 3
Views: 3247
Reputation: 19600
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()
Upvotes: 1