我想從 DataFrame 創(chuàng)建一個(gè) HTML 表。表的列數(shù)是固定的(不會(huì)改變)。但表的行號(hào)總是在變化。我簡(jiǎn)化了我的代碼,以便任何 csv 文件都足以重現(xiàn)該問(wèn)題。我的代碼來(lái)自這里注意:我將設(shè)置 HTML 代碼的樣式(給單元格著色、更改字體大小等),我知道DataFrame.to_html功能。但不可能用它來(lái)設(shè)置 HTML 代碼的樣式。所以我沒(méi)有使用它。這是我嘗試過(guò)的:import pandas as pddata=pd.read_csv("auto.csv") data = data.iloc[:3, : 7] #get first 3 rows and 7 columnsprint(data.describe)# Send emailtable = []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))這就是結(jié)果<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>
如何從 DataFrame 創(chuàng)建 HTML 表
慕標(biāo)5832272
2023-11-09 15:44:30