Here are few different approaches to create a hyperlink in Pandas DataFrame and JupyterLab:

(1) Pandas method: to_html and parameter render_links=True:

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

(2) Create clickable link from single column in Pandas DataFrame:

def make_clickable(val):
    return f'<a target="_blank" href="{val}">{val}</a>'

df.style.format({'url': make_clickable})

(3) Create hyperlink from name and URL in Pandas with apply:

df['name'] = df['name'].apply(lambda x: f'<a href="http://softhints.com/tutorial/{x}">{x}</a>')
HTML(df.to_html(escape=False))

In the next section, We'll review the steps to apply the above examples in practice.

The first example to cover will create links from all columns which contain valid URL-s with option - render_links.

Parameter render_links is from boolean type and has default False.

Convert URLs to HTML links.

So let's create a DataFrame with two columns:

  • name
  • url
  • ur2
from IPython.display import HTML
import pandas as pd

df = pd.DataFrame({
    'name':['Softhints', 'DataScientyst'],
    'url':['https://www.softhints.com', 'https://datascientyst.com'],
    'url2':['https://www.blog.softhints.com/tag/pandas', 'https://datascientyst.com/tag/pandas']
})

to render all links from this DataFrame we can use the next syntax:

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

DataFrame contains hyperlinks:

name url url2
0 Softhints https://www.softhints.com https://www.blog.softhints.com/tag/pandas
1 DataScientyst https://datascientyst.com https://datascientyst.com/multiindex

As you can see from the table above all URLs are rendered as links.

In this example we can see how to create a method which is going to convert:

  • name
  • url

from two columns of Pandas DataFrame to a new column with a short hyperlink.

Let's say that our DataFrame has two values - name and url .

df = pd.DataFrame({
    'name':['Softhints', 'DataScientyst'],
    'url':['https://www.softhints.com', 'https://datascientyst.com']
})

To create a new column which is a shortened hyperlink we can make a function. Then we can use method format of style in order to apply the function to different columns - given as a dict:

def make_clickable(val):
    return f'<a target="_blank" href="{val}">{val}</a>'

df.style.format({'url': make_clickable})

result:

  name url
0 Softhints https://www.softhints.com
1 DataScientyst https://datascientyst.com

To use only the URL without the name we can build another function:

def make_clickable(val):
    return f'<a href="{val}">{val}</a>'

df.style.format(make_clickable)

Creating a new clickable column which is a combination from the other columns of the same DataFrame:

df['link'] = df.apply(lambda x: make_clickable(x['url'], x['name']), axis=1)
df.style

Finally, let's see how to combine lambda and to_html(escape=False) to create a clickable link in DataFrame.

from IPython.display import HTML

df = pd.DataFrame({'name':['Pandas', 'Linux']})

df['name'] = df['name'].apply(lambda x: f'<a href="http://softhints.com/tutorial/{x}">{x}</a>')
HTML(df.to_html(escape=False))

As a result the column URL has clickable values:

name
0 Pandas
1 Linux

Resources