How to edit table so that index and column names are on the same row.

import beatifulsoup.

from bs4 import BeautifulSoup

find the empty tags in ‘thead’ and then fill with col names from the dataframe.
Note: If you code “header=False”, the default style for table row will be gone
<tr style=”text-align: right;”>
html_doc = tide_table.to_html(header=False)
soup = BeautifulSoup(html_doc, 'html.parser')

empty_cols = soup.find('thead').find_all(lambda tag: not tag.contents)

for tag, col in zip(empty_cols, tide_table):
    tag.string = col

with open(self.station_id + "_tide_table.html", "w") as f:
    f.write(soup.decode_contents())

 

Leave a Reply