0

I have a pandas dataframe which looks something like the below...

enter image description here

I am aware that i am unable to merge cells of the dataframe itself, but is there a simple way to merge the cells of Column1 in the HTML output so that i get something like the below?...

desired images

dboy
  • 1,004
  • 2
  • 16
  • 24
Harry
  • 25
  • 4
  • Does this answer your question? [Pandas Data Frame how to merge columns](https://stackoverflow.com/questions/49533330/pandas-data-frame-how-to-merge-columns) – mij Apr 14 '20 at 10:48

1 Answers1

0

This is a bit hacky (as I parse the html text) but may work for your particular case...

df = pd.DataFrame({
    'col1': ['A', 'A', 'B', 'B'],
    'col2': [1, 2, 3, 4],
    'col3': [5, 6, 7, 8]})

df['cleanme'] = 'cleanme'

df = df.set_index(['col1', 'cleanme'])

html = df.to_html(index_names=False)
html = re.sub('<th></th>\n.*<th></th>', '<th>col1</th>', html)  # notice your column name here
html = re.sub('<th>cleanme</th>', '', html)

with open('index.html', 'w') as fou:
    fou.write(html)
dmontaner
  • 2,076
  • 1
  • 14
  • 17