Python 在表中插入链接

Python 在表中插入链接,python,pandas,ipython-notebook,Python,Pandas,Ipython Notebook,我想在pandas表中插入一个链接(指向网页),这样当它显示在ipython笔记本中时,我就可以按链接了 我尝试了以下方法: 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

我想在pandas表中插入一个链接(指向网页),这样当它显示在ipython笔记本中时,我就可以按链接了

我尝试了以下方法:

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
但是url只是显示为文本

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

我想你必须把整个熊猫对象表示为

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))
[1]中的
:从IPython.display导入HTML
在[2]中:df=pd.DataFrame(列表(范围(5)),列=['a'])
在[3]中:df['a']=df['a'].apply(lambda x:''。格式(x))
在[4]中:HTML(df.to_HTML(escape=False))

很抱歉,现在手头没有IPython,无法检查输出是否正确。

因为版本24 pandas有一种本机方式来处理链接:

这项工作:

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,
)
df[“col”]=df[“col”]。应用(#插入链接
lambda x:“”.格式(
关于findall(“模式”,x)[0],x
)
)
df.to_html(
render_links=True,
逃逸=假,
)

如果您想避免缩短长URL的问题,您还可以显示具有唯一或标准值的链接,即

df['Url'] = '<a href=' + df['Url'] + '><div>' + df['Name'] + '</div></a>'

df = df.to_html(escape=False)

# OR

df['Url'] = '<a href=' + df['Url'] + '><div>'Hello World'</div></a>'

df = df.to_html(escape=False)
df['Url']='
df=df.to_html(escape=False)
#或
df['Url']='
df=df.to_html(escape=False)

我可以确认它是否有效(使用anconda 4.2.0)!谢谢@alko。我注意到,对于有些长的URL,pandas“无声地”缩短html,以便在单元格中显示“空白”。这里有一个解决方案:此方法截断任何长度超过35个字符的链接,这意味着链接渲染无法工作。。。你知道怎么解决这个问题吗?/我能在这里找到答案
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))
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,
)
df['Url'] = '<a href=' + df['Url'] + '><div>' + df['Name'] + '</div></a>'

df = df.to_html(escape=False)

# OR

df['Url'] = '<a href=' + df['Url'] + '><div>'Hello World'</div></a>'

df = df.to_html(escape=False)