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.

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 theto_htmlsource 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.