1

I was hoping that it was possible to format the index (column) directly in connection with output to HTML with the to_html() in the Pandas DataFrame?

Something like:

df = DataFrame([[1, 2]], index=['a'], columns=['A', 'B'])
print(df.to_html(formatters={
    'index': lambda elem: '<a href="example.com/{}">{}</a>'.format(elem, elem)}, escape=False))

This does not work. I do not get a link.

I suppose I would need to do something like

dfc = df.copy()
dfc.index = ['<a href="example.com/{}">{}</a>'.format(i, i) for i in df.index]
print(dfc.to_html(escape=False))

1 Answer 1

4

you should use __index__ instead of index:

print(df.to_html(formatters={
    '__index__': lambda elem: '<a href="example.com/{}">{}</a>'.format(elem, elem)}, escape=False))

Source: The pull request that introduced that feature.

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.