如何在pandas和Jupyter Notebook中创建一个带有可点击超链接的表

zad*_*zny 17 pandas jupyter-notebook

print('http://google.com') 输出可点击的网址.

如何获取可点击的网址pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])

dlo*_*ckx 31

如果要仅将URL格式应用于单个列,可以使用:

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})
Run Code Online (Sandbox Code Playgroud)

(PS:不幸的是,我没有足够的声誉将这个评论发布给@Abdou的帖子)

  • @shantanuo这样的事情:`df ['nameurl'] = df ['name'] +'#'+ df ['url']`,`def make_clickable_both(val):name,url = val.split('# '),返回f'<a href="{url}"> {name} </a>'`,`df.style.format({'nameurl':make_clickable_both})` (3认同)
  • 如何使“名称”可点击并隐藏网址? (2认同)

Abd*_*dou 18

试着用pd.DataFrame.style.format这个:

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)
Run Code Online (Sandbox Code Playgroud)

我希望这证明是有用的.

  • 你可以使用`'&lt;a href="{0}"&gt;{0}&lt;/a&gt;'.format(val)` (3认同)

Duf*_*ffJ 9

我在How to Create a Clickable Link(s) in Pandas DataFrame and JupyterLab 中找到了这个,它解决了我的问题:

HTML(df.to_html(render_links=True, escape=False))
Run Code Online (Sandbox Code Playgroud)

  • 就我而言(JupyterNotebook),它需要“from IPython.display import HTML” (3认同)

小智 6

@shantanuo:没有足够的声誉发表评论。以下情况如何?

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)

Run Code Online (Sandbox Code Playgroud)


小智 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)))
Run Code Online (Sandbox Code Playgroud)