print('http://google.com')
outputs a clickable url.
How do I get clickable URLs for pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])
?
print('http://google.com')
outputs a clickable url.
How do I get clickable URLs for pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])
?
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.
apply..
then just df.style
suffices –
Skillern f"<a href="{val}">{val}</a>"
–
Williams 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)
df['nameurl'] = df['name'] + '#' + df['url']
, def make_clickable_both(val): name, url = val.split('#'), return f'<a href="{url}">{name}</a>'
, df.style.format({'nameurl': make_clickable_both})
–
Abhor new_df = df[['nameurl']]
to create a dataframe with only the nameurl
column. –
Abhor pandas.DataFrame.to_html()
. See also #49904273 . –
Abhor 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.
'<a href="{0}">{0}</a>'.format(val)
–
Constitutional apply..
then just df.style
suffices –
Skillern f"<a href="{val}">{val}</a>"
–
Williams 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))
from IPython.display import HTML
–
Sjoberg @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)
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)))
© 2022 - 2024 — McMap. All rights reserved.
'<a href="{0}">{0}</a>'.format(val)
– Constitutional