如何在 Pandas & Jupyter Notebook 中创建带有可点击超链接的表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42263946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:59:35  来源:igfitidea点击:

How to create a table with clickable hyperlink in pandas & Jupyter Notebook

pandasjupyter-notebook

提问by zadrozny

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

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

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

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

回答by dloeckx

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

如果您只想将 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})

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

(PS:不幸的是,我没有足够的声誉将此作为对@Abdou 帖子的评论发布)

回答by Abdou

Try using pd.DataFrame.style.formatfor this:

尝试使用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)

I hope this proves useful.

我希望这证明是有用的。

回答by nicolauga

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

@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)