Insert a link inside a pandas table

I suppose you have to represent whole Pandas object as an HTML object, that is

In [1]: from IPython.display import HTML

In [2]: df = pd.DataFrame(list(range(5)), columns=['a'])

In [3]: df['a'] = df['a'].apply(lambda x: '<a href="http://example.com/{0}">link</a>'.format(x))

In [4]: HTML(df.to_html(escape=False))

Sorry, now I don't have IPython at hand, and can't check whether the output is correct.


If you want to avoid the issue of shortening the long urls you can also display the links with unique or standard values i.e.

df['Url'] = '<a href=' + df['Url'] + '><div>' + df['Name'] + '</div></a>'

df = df.to_html(escape=False)

# OR

df['Url'] = '<a href=' + df['Url'] + '><div>'Hello World'</div></a>'

df = df.to_html(escape=False)

Since version 24, Pandas has a native way to deal with links: pandas.DataFrame.to_html

This works:

df["col"] = df["col"].apply( # insert links
            lambda x: "<a href='https://link{}'>{}</a>".format(
                re.findall("pattern", x)[0], x
            )
        )

df.to_html(
    render_links=True,
    escape=False,
)