pandas 在熊猫表中插入链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20035518/
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
Insert a link inside a pandas table
提问by lev
I'd like to insert a link (to a web page) inside a pandas table, so when it is displayed in ipython notebook, I could press the link.
我想在 Pandas 表中插入一个链接(到网页),所以当它显示在 ipython notebook 中时,我可以按下链接。
I tried the following:
我尝试了以下方法:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame(range(5), columns=['a'])
In [3]: df['b'] = df['a'].apply(lambda x: 'http://example.com/{0}'.format(x))
In [4]: df
Out[4]:
a b
0 0 http://example.com/0
1 1 http://example.com/1
2 2 http://example.com/2
3 3 http://example.com/3
4 4 http://example.com/4
but the url is just displayed as text.
但网址仅显示为文本。
I also tried using ipython HTML object:
我也尝试使用 ipython HTML 对象:
In [5]: from IPython.display import HTML
In [6]: df['b'] = df['a'].apply(lambda x:HTML('http://example.com/{0}'.format(x)))
In [7]: df
Out[7]:
a b
0 0 <IPython.core.display.HTML object at 0x0481E530>
1 1 <IPython.core.display.HTML object at 0x0481E770>
2 2 <IPython.core.display.HTML object at 0x0481E7B0>
3 3 <IPython.core.display.HTML object at 0x0481E810>
4 4 <IPython.core.display.HTML object at 0x0481EA70>
but it will only display the repr of the object.
但它只会显示对象的代表。
Any other ideas?
还有其他想法吗?
EDIT: alko got the right answer, just wanted to add that the cell width is limited by default, and long html code will be truncated, ie:
编辑: alko 得到了正确的答案,只是想补充一点,默认情况下单元格宽度是有限的,并且长的 html 代码将被截断,即:
<a href="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0">xxx</a>
will become this:
会变成这样:
<a href="aaaaaaaaaaaaaaaaaaaaaa...
and won't be displayed correctly. (even though the text xxx is short and can fit in the cell)
并且不会正确显示。(即使文本 xxx 很短并且可以放入单元格中)
I've bypassed it by setting:
我通过设置绕过了它:
pd.set_printoptions(max_colwidth=-1)
回答by alko
I suppose you have to represent whole pandas object as html object, that is
我想你必须将整个 Pandas 对象表示为html 对象,即
In [1]: from IPython.display import HTML
In [2]: df = pd.DataFrame(list(range(5)), columns=['a'])
In [3]: df['a'] = df['a'].apply(lambda x: '<a href="http://example.com/{0}">link</a>'.format(x))
In [4]: HTML(df.to_html(escape=False))
Sorry, now don't have IPython at hand, and can't check whether output is correct.
抱歉,现在手头没有IPython,无法检查输出是否正确。
回答by Claudiu Creanga
Since version 24 pandas has a native way to deal with links: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html
由于版本 24 Pandas有一种本地方式来处理链接:https: //pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html
This works:
这有效:
df["col"] = df["col"].apply( # insert links
lambda x: "<a href='https://link{}'>{}</a>".format(
re.findall("pattern", x)[0], x
)
)
df.to_html(
render_links=True,
escape=False,
)

