andrews
andrews

Reputation: 41

How do you merge headers on plotly tables using pandas?

Is there any way using Plotly Pandas package to merge the duplicated header rows within a table?

I know there is a way using plotly dash (with the merge_duplicate_headers=True command) but it doesn't seem to translate

The table currently has two header lines, I want to merge the duplicated top line

This is the expected outcome (replicated in Excel)

table_1 = go.Table(
    header=dict(
        values=[['test_1','day 1'], ['test_1','year'],['test_2','day 1'], ['test_2','year']],
    ),
    cells=dict(values=[[1,2,3,4,5], [2014,2015,2016,2017,2018],[6,7,8,9,10], [2014,2015,2016,2017,2018]]),
)

fig = dict(data=table_1)
py.iplot(fig)

Upvotes: 4

Views: 4198

Answers (1)

rpanai
rpanai

Reputation: 13437

Have you considered use a pd.DataFrame and pass it to plotly?

import pandas as pd
import plotly.graph_objects as go
import plotly.offline as py

df = pd.DataFrame([[1,2,3,4,5], [2014,2015,2016,2017,2018],
                   [6,7,8,9,10], [2014,2015,2016,2017,2018]]).T

df.columns = pd.MultiIndex.from_tuples([['test_1', 'day 1'], ['test_1', 'year'],
                                        ['test_2', 'day 1'], ['test_2', 'year']])

df.columns = ["_".join(col) for col in df.columns]

table_1 = go.Table(
    header=dict(
        values=df.columns,
    ),
    cells=dict(values=df.values.T),
)

fig = dict(data=table_1)
py.iplot(fig)

Upvotes: 1

Related Questions