0

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 Answer 1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.