I'd like to insert a link (to a web page) inside a Pandas table, so when it is displayed in an IPython notebook, I could press the link.
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 an IPython HTML object:
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?
alko got the right answer. I just wanted to add that the cell width is limited by default, and long HTML code will be truncated, i.e.:
<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.)
I've bypassed it by setting:
pd.set_printoptions(max_colwidth=-1)
question from:
https://stackoverflow.com/questions/20035518/insert-a-link-inside-a-pandas-table 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…