Unable to convert pandas header alone to html. I have below code,
df = pd.read_csv("Employees.csv") df1= df.columns.to_frame() df1.to_html("upload.html")
Result of the code is ,
Expected result is,
Unable to get the header alone from the index of the data frame. Any suggestion would be appreciated.
3
Customised the code to generate html attributes. This is more efficient as we can also add CSS and other html properties.
file = open("templates/upload.html", "w") table = ["""<table border="1"> n"""] file.writelines(table) file.close() file = open("templates/upload.html", "a") for row in list(df.columns): header = """<tr style="text-align: left;">n<th>""" + row + "</th>n</tr>n" file.writelines(header) file.close() file = open("templates/upload.html", "a") table = ["</table>"] file.writelines(table) file.close()
You can set headers to false to get rid of the top row. And index to false to get rid of the first column.
headers
index
df1.to_html("upload.html", header=False, index=False)
You’re left with the right column, which you can add styles to make it bold and centred like the left column.
Documentation
Hope this helps!
Use df.head(0).to_html(header=True, index=False) to get just the headers.
df.head(0).to_html(header=True, index=False)
Click here to cancel reply.
3
Answers
Customised the code to generate html attributes. This is more efficient as we can also add CSS and other html properties.
You can set
headers
to false to get rid of the top row. Andindex
to false to get rid of the first column.You’re left with the right column, which you can add styles to make it bold and centred like the left column.
Documentation
Hope this helps!
Use
df.head(0).to_html(header=True, index=False)
to get just the headers.