7

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.

1
  • 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, 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. Commented Jul 26, 2019 at 10:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.