如何从 Pandas DataFrame 输出带有合并单元格的 html 表 [英] How to output html table with merged cells from pandas DataFrame

查看:54
本文介绍了如何从 Pandas DataFrame 输出带有合并单元格的 html 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pandas.DataFrame df 为:

<预><代码>>>>df = pd.DataFrame([[1,2,2,2,3], [1,2,3,3,3],[1,3,2,3,5],[7,9,9,3,2]],列=列表(ABCDE"))

我想在html中实现这种类型的表格(控制我可以合并哪些单元格)

我知道可以通过操作从 df.to_html() 函数获得的表并使用 jquery 来扩展行跨度来实现,但是 我正在询问 pythonic 方式 来做到这一点,即是否有可能直接从某种数据透视表/数据框中获取合并表.

我考虑过将列临时设置为在多索引数据框中合并为索引,但是这种方法至少可以说是..粗糙的.

如果我可以完全控制我可以合并哪些单元格,例如,基于同一行中其他单元格的值,那将是完美的.

解决方案

更新:我设法找到了 类似的问题,有广泛的答案(见 jpp 的答案),但不幸的是否定了我的问题的简单解决方案的可能性.正如 silkworm 所建议的那样,目前唯一的可能是钻研 to_html 源,或者干预多索引数据框.

I have a pandas.DataFrame df as:

>>> df = pd.DataFrame([[1,2,2,2,3], [1,2,3,3,3],[1,3,2,3,5],[7,9,9,3,2]], columns=list("ABCDE"))

I want to achieve this type of table in html (with control of which cells I can merge)

I know that it can be achieved manipulating the table obtained from df.to_html() function and using jquery to expand the rowspans, yet I'm asking about the pythonic way to do it, i.e. is there a possible way of obtaining the merged table directly from some sort of pivot table / dataframe.

I thought about temporary setting the columns to merge as indexes in multi indexed data frame, however this approach is.. crude, to say the least.

It would be perfect if I had the full control of which cells can I merge, based on, for example, values of other cells in the same row.

解决方案

UPDATE: I've managed to find similar question, with extensive answer (see jpp's answer), yet unfortunately negating possibility of simple solution to my problem. As silkworm suggested, for now the only possibility is to delve the to_html source, or meddling with multi indexing the dataframe.

这篇关于如何从 Pandas DataFrame 输出带有合并单元格的 html 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆