47

print('http://google.com') outputs a clickable url.

How do I get clickable URLs for pd.DataFrame(['http://google.com', 'http://duckduckgo.com']) ?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
zadrozny
  • 1,631
  • 3
  • 22
  • 27

5 Answers5

78

If you want to apply URL formatting only to a single column, you can use:

data = [dict(name='Google', url='http://www.google.com'),
        dict(name='Stackoverflow', url='http://stackoverflow.com')]
df = pd.DataFrame(data)

def make_clickable(val):
    # target _blank to open new window
    return '<a target="_blank" href="{}">{}</a>'.format(val, val)

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

(PS: Unfortunately, I didn't have enough reputation to post this as a comment to @Abdou's post)

ihightower
  • 3,093
  • 6
  • 34
  • 49
dloeckx
  • 1,110
  • 1
  • 10
  • 8
  • 3
    How do I make the "name" clickable and hide the url? – shantanuo May 07 '18 at 06:12
  • 7
    @shantanuo Something like this: `df['nameurl'] = df['name'] + '#' + df['url']`, `def make_clickable_both(val): name, url = val.split('#'), return f'{name}'`, `df.style.format({'nameurl': make_clickable_both})` – dloeckx May 08 '18 at 06:55
  • Great stuff! Question: is it possible then to delete the 'name' and 'url' columns from df or can we create a new pandas data frame with only the 'nameurl' column? – Alan Mar 15 '20 at 19:42
  • @Alan : yes, only the nameurl column is needed. So you can e.g. use `new_df = df[['nameurl']]` to create a dataframe with only the `nameurl` column. – dloeckx Mar 22 '20 at 21:12
  • @ dloeckx Thanks! – Alan Mar 24 '20 at 23:53
  • @dloeckx .. If we have multiple columns in a dataframe , how to display complete dataframe in a web page (or html table to get displayed in hyper link) – Ravi Oct 30 '20 at 16:07
  • @Ravi Take a look at `pandas.DataFrame.to_html()`. See also https://stackoverflow.com/questions/49903773/pandas-dataframe-to-html-formatting-the-values-to-display-centered . – dloeckx Nov 02 '20 at 10:06
  • @dloeckx .. Thanks for the reply.. I am trying to display the html table in an url. I want to create a url and display this html data in that url , can you please help me if there is any link i can refer and try – Ravi Nov 02 '20 at 10:14
  • @Ravi To be able to generate a URL and display this HTML data in that URL is beyond the reach of Pandas afaik. You can take a look at [Flask](https://flask.palletsprojects.com/) to setup a web server, or save the file to a directory that is served to the web. Note that if you would like to make the link public, you might open a lot of security holes! – dloeckx Nov 03 '20 at 16:57
47

Try using pd.DataFrame.style.format for this:

df = pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])

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

df.style.format(make_clickable)

I hope this proves useful.

Abdou
  • 12,931
  • 4
  • 39
  • 42
8

@shantanuo : not enough reputation to comment. How about the following?

def make_clickable(url, name):
    return '<a href="{}" rel="noopener noreferrer" target="_blank">{}</a>'.format(url,name)

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

Robert Brown
  • 10,888
  • 7
  • 34
  • 40
nicolauga
  • 89
  • 1
  • 2
8

I found this at How to Create a Clickable Link(s) in Pandas DataFrame and JupyterLab which solved my problem:

HTML(df.to_html(render_links=True, escape=False))
Dawngerpony
  • 3,288
  • 2
  • 34
  • 32
5
from IPython.core.display import display, HTML
import pandas as pd

# create a table with a url column
df = pd.DataFrame({"url": ["http://google.com", "http://duckduckgo.com"]})

# create the column clickable_url based on the url column
df["clickable_url"] = df.apply(lambda row: "<a href='{}' target='_blank'>{}</a>".format(row.url, row.url.split("/")[2]), axis=1)

# display the table as HTML. Note, only the clickable_url is being selected here
display(HTML(df[["clickable_url"]].to_html(escape=False)))
Frederico Wu
  • 61
  • 1
  • 1