问题
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