Render pandas dataframe to HTML table with row seperators

笑着哭i 提交于 2021-01-29 15:02:15

问题


I render a pandas multi-index into an html table.

When rendering, after I passed a specific index X I want to add a separator line into the table. I can do this manually by editing the final html file:

/* .... Index X .... */
<tr>
                <td colspan="20" class="divider"><hr /></td>
</tr>
/* .... Index Y .... */

This produces the desired result:

.

Question Is there any way to capture this in code?
I want to avoid going through each HTML table I create, adding those lines manually.


回答1:


You can groupby index and loop through it while adding the separator:

df = pd.DataFrame({"col1":list("AABBCCA"),"unit":[1,2,1,3,4,4,6]})

for _, data in df.groupby("col1"):
    print (data.to_html())
    print ('<td colspan="20" class="divider"><hr /></td>')

Result:



来源:https://stackoverflow.com/questions/60524944/render-pandas-dataframe-to-html-table-with-row-seperators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!