如何使用 Pandas 在 Plotly 表格中合并表头?

3
有没有办法使用 Plotly Pandas 包来合并表格中的重复表头行?我知道可以使用 plotly dash(使用 merge_duplicate_headers=True 命令),但好像无法实现。该表格目前有两个表头行,我想要合并顶部的重复行。期望输出结果如下(在 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)

2
你能提供一些样本输出或任何当前输出的截图吗? - amrs-tech
嗨,我已经在说明链接中嵌入了相关输出的图片。谢谢! - andrews
你读过这个了吗? - rpanai
你期望的输出是什么?你所说的“合并”是什么意思? - Allen Qin
1
@rpanai,很遗憾这不是我想要的答案,但其他人可能会发现它有用。 - andrews
显示剩余3条评论
1个回答

1
你考虑过使用 pd.DataFrame 并将其传递给 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接