Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clickable link in pandas dataframe

Tags:

pandas

This code works correctly and creates the clickable URL. But I will like to make the "name" column clickable and hide url. Is it possible?

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

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

df.style.format({'url': make_clickable})
like image 239
shantanuo Avatar asked Oct 14 '25 09:10

shantanuo


1 Answers

df['nameurl'] = df['name'] + '#' + df['url']

def make_clickable_both(val): 
    name, url = val.split('#')
    return f'<a href="{url}">{name}</a>'

df.reset_index().style.format({'nameurl': make_clickable_both})
like image 146
shantanuo Avatar answered Oct 17 '25 04:10

shantanuo