> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Create a Clickable Link(s) in Pandas DataFrame and JupyterLab
- URL: https://datascientyst.com/create-clickable-link-pandas-dataframe-jupyterlab/
- Published: 2021-09-07T12:56:01.000Z
- Updated: 2022-06-21T06:38:16.000Z
- Author: John D K
- Tags: Display

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

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

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

```

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

```python
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`**:

```python
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.

## Step 1: Create a Hyperlink with `to_html` and `render_links` \- multiple columns

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

```python
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:

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

```

DataFrame contains hyperlinks:

|   | name          | url                                                                           | url2                                                                                                         |
| - | ------------- | ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| 0 | Softhints     | [https://www.softhints.com](https://www.softhints.com/?ref=datascientyst.com) | [https://www.blog.softhints.com/tag/pandas](https://www.blog.softhints.com/tag/pandas?ref=datascientyst.com) |
| 1 | DataScientyst | [https://datascientyst.com](https://datascientyst.com/)                       | <https://datascientyst.com/multiindex>                                                                       |

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

## Step 2: Create a new hyperlink column as combination of others columns in Pandas

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 .

```python
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:

```python
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](https://www.softhints.com/?ref=datascientyst.com) |
| 1 | DataScientyst | [https://datascientyst.com](https://datascientyst.com/)                       |

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

```python
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**:

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

```

## Step 3: Create hyperlink from URL using lambda and to\_html(escape=False)

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

```python
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](http://softhints.com/tutorial/Pandas?ref=datascientyst.com) |
| 1 | [Linux](http://softhints.com/tutorial/Linux?ref=datascientyst.com)   |

## Resources

- [Notebook](https://github.com/softhints/Pandas-Tutorials/blob/master/styling/create-clickable-link-pandas-dataframe-jupyterlab.ipynb?ref=datascientyst.com)
- [pandas.DataFrame.to\_html](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to%5Fhtml.html?ref=datascientyst.com)