2

I would like to create a HTML table from a DataFrame. Column number of table is fixed (it doesn't change). But row number of table always changes. I simplified my code so that any csv file will enough to reproduce the problem. My code is come from here

Note: I will style HTML code (coloring cells, changing font sizes etc.) I know DataFrame.to_html function. But it is not possible to style HTML code with that. So I didn't use it.

Here is what I've tried:

import pandas as pd

data=pd.read_csv("auto.csv") 
data = data.iloc[:3, : 7] #get first 3 rows and 7 columns
print(data.describe)

# Send email
table = []
table.append("<table>\n")
for obj in data:
    table.append("\t<tr>\n")
    td = []
    for key in dir(obj)[:7]:
        td.append("<td>{0}</td>".format(key))
    table.append("\t\t"+"".join(td))
    table.append("\n\t</tr>\n")

table.append("</table>")

print("".join(table))

And this is the result

<table>
        <tr>
                <td>__add__</td><td>__class__</td><td>__contains__</td><td>__delattr__</td><td>__dir__</td><td>__doc__</td><td>__eq__</td>
        </tr>
        <tr>
                <td>__add__</td><td>__class__</td><td>__contains__</td><td>__delattr__</td><td>__dir__</td><td>__doc__</td><td>__eq__</td>
        </tr>
        <tr>
                <td>__add__</td><td>__class__</td><td>__contains__</td><td>__delattr__</td><td>__dir__</td><td>__doc__</td><td>__eq__</td>
        </tr>
        <tr>
                <td>__add__</td><td>__class__</td><td>__contains__</td><td>__delattr__</td><td>__dir__</td><td>__doc__</td><td>__eq__</td>
        </tr>
        <tr>
                <td>__add__</td><td>__class__</td><td>__contains__</td><td>__delattr__</td><td>__dir__</td><td>__doc__</td><td>__eq__</td>
        </tr>
        <tr>
                <td>__add__</td><td>__class__</td><td>__contains__</td><td>__delattr__</td><td>__dir__</td><td>__doc__</td><td>__eq__</td>
        </tr>
        <tr>
                <td>__add__</td><td>__class__</td><td>__contains__</td><td>__delattr__</td><td>__dir__</td><td>__doc__</td><td>__eq__</td>
        </tr>
</table>

This is the expected result:

<table>
        <tr>
                <td>mpg</td><td>cylinders</td><td>displacement</td><td>horsepower</td><td>weight</td><td>acceleration</td><td>_year</td>
        </tr>
        <tr>
                <td>18</td><td>8</td><td>307</td><td>130</td><td>3504</td><td>12</td><td>70</td>
        </tr>
        <tr>
                 <td>28</td><td>38</td><td>207</td><td>110</td><td>3074</td><td>10</td><td>90</td>
        </tr>
</table>
Ronnie
  • 23
  • 3

1 Answers1

1

See DataFrame.to_html

Render a DataFrame as an HTML table.

from io import StringIO
import pandas as pd

styles = '''\
<style>
td {background-color: tan}
</style>
'''

data=pd.read_csv("auto.csv") 

buff = StringIO()
data.to_html(buff)
html_string = buff.getvalue()
print(styles + html_string)
  • I know `DataFrame.to_html`. But I will style HTML code. If I use `DataFrame.to_html` it is not possible to style HTML code. Isn't it? – Ronnie Oct 14 '20 at 17:42
  • What's stopping you from prefixing the HTML table with `` and doing the formatting there? E.g., if I prefix the generated HTML with `` then I get cells with colored background. –  Oct 14 '20 at 17:46