如何在pandas&;中创建具有可单击超链接的表;Jupyter笔记本
如何在pandas&;中创建具有可单击超链接的表;Jupyter笔记本,pandas,jupyter-notebook,Pandas,Jupyter Notebook,print('http://google.com)输出可单击的url 如何获取pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])?尝试使用以下方法: df = pd.DataFrame(['http://google.com', 'http://duckduckgo.com']) def make_clickable(val): return '<a href="{}">{}</a>'.for
print('http://google.com)
输出可单击的url
如何获取pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])
?尝试使用以下方法:
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)
df=pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])
def使_可点击(val):
返回“”。格式(val,val)
df.style.format(使可点击)
我希望这会很有用。如果您只想对单个列应用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})
data=[dict(name='Google',url='1]http://www.google.com'),
dict(name='Stackoverflow',url='Stackoverflow'http://stackoverflow.com')]
df=pd.DataFrame(数据)
def使_可点击(val):
#目标为空以打开新窗口
返回“”。格式(val,val)
format({'url':使_可点击})
(注:不幸的是,我没有足够的声誉将此作为评论发布到@Abdou的帖子上)@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)
def使可点击(url、名称):
返回“”。格式(url、名称)
df['name']=df.apply(lambda x:make_clickable(x['url'],x['name']),axis=1)
来自IPython.core.display导入显示,HTML
作为pd进口熊猫
#创建具有url列的表
df=pd.DataFrame({“url”:[“http://google.com", "http://duckduckgo.com"]})
#基于url列创建列可单击的url
df[“clickable_url”]=df.apply(lambda行:”.format(row.url,row.url.split(“/”[2]),axis=1)
#将表格显示为HTML。注意,此处仅选择可单击的url
显示(HTML(df[[“可点击的\u url”]]到\u HTML(escape=False)))
我如何使“name”可点击并隐藏url?@shantanuo类似这样:df['nameurl']=df['name']+'.''.'''.++df['url'.]
,def make_clickable_两个(val):name,url=val.split('.'.''''.'),返回f'
,df style.format({'nameurl':make_clickable_两个})
很棒的东西!问:那么是否可以从df中删除“name”和“url”列,或者我们是否可以创建一个只包含“nameurl”列的新熊猫数据框?@Alan:是的,只需要nameurl列。例如,您可以使用new_df=df[['nameurl']]
创建一个只包含nameurl
列的数据框。@dloeckx谢谢!你可以使用'.format(val)
放在旁边注:首先通过apply..
添加列,然后只需df.style
就足够了,或者Python3你可以使用f”“
这看起来很棒。作为附录,我如何创建一个可点击的链接来执行我在别处定义的底层功能?
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)))