简体   繁体   中英

How to output html table with merged cells from pandas DataFrame

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, ie 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.

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