'How to export Pandas DataFrame to HTML but without any formatting?
I want to export a DF with Pandas to an HTML formatted table, but I don't want any of the default styling that Pandas does to its tables, and would prefer just a bone-stock table. Is there an easy way to do this when using the to_html function?
There isn't really a Minimal Reproducible Example since it is just one line of code, I just want
html_file = df.to_html()
to make my HTML file table go from
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
to
<table>
<thead>
<tr>
Solution 1:[1]
For me working remove attributes after generate html
:
df = pd.DataFrame(
{
"a": [1]
})
from bs4 import BeautifulSoup
soup = BeautifulSoup(df.to_html(), features="lxml")
for tag in soup.find_all(True):
tag.attrs.clear()
print(soup.prettify())
<html>
<body>
<table>
<thead>
<tr>
<th>
</th>
<th>
a
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
0
</th>
<td>
1
</td>
</tr>
</tbody>
</table>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jezrael |