5

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"))

dataframe presented as table

I want to achieve this type of table in html (with control of which cells I can merge) dataframe presented as table after 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.

errno98
  • 312
  • 2
  • 12
  • I can't think of any obvious way to do this. If you run `df.set_index(list('ABCDE')).to_html('test.html')`, you'll see that merging does exist for index cells (but only in cases where everything to the left matches too). I mention this not because it solves your problem, but maybe if you want to take a look into the `to_html` [source code](https://github.com/pandas-dev/pandas/blob/01babb590cb15ef5c6e9ad890ea580a5112e6999/pandas/io/formats/html.py), you can find the code that is responsible for this merging of similar cells in the index output and you can tweak it to suit your needs. – sjw Jul 26 '19 at 10:54

1 Answers1

3

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.

errno98
  • 312
  • 2
  • 12