【问题标题】:Insert a link inside a Pandas table在 Pandas 表中插入链接
【发布时间】:2013-11-30 21:05:50
【问题描述】:

我想在 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>

但它只会显示对象的repr。

还有其他想法吗?


alko 得到了正确的答案。我只是想补充一点,默认情况下单元格宽度是有限的,长HTML代码会被截断,即:

<a href="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0">xxx</a>

会变成这样:

<a href="aaaaaaaaaaaaaaaaaaaaaa...

并且不会正确显示。 (即使文本 xxx 很短并且可以放入单元格中。)

我已经通过设置绕过它:

pd.set_printoptions(max_colwidth=-1)

【问题讨论】:

    标签: python pandas jupyter-notebook


    【解决方案1】:

    我想你必须将整个 Pandas 对象表示为HTML object,即

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

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

    【讨论】:

    • 我可以确认它确实有效(使用 anconda 4.2.0)!谢谢@alko。
    • 我注意到对于有些长的 url,pandas 会“默默地”缩短 html 以便在单元格中显示“空白”。这是一个解决方案:stackoverflow.com/q/26277757/621449
    • 这个方法会截断任何超过 35 个字符的链接,这意味着链接渲染不起作用......知道如何解决这个问题/
    【解决方案2】:

    从版本 24 开始,Pandas 有了处理链接的原生方式:pandas.DataFrame.to_html

    这行得通:

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

    【讨论】:

      【解决方案3】:

      安装漂亮的html表

      from pretty_html_table import build_table
      
      body = """
      <html>
      <head>
      </head>
      
      <body>
              {0}
      </body>
      
      </html>
      """.format(build_table(df, 'blue_light'))
      

      您不必担心 DataFrame 中网站链接的格式和格式,输出将仅包含超链接。

      【讨论】:

        【解决方案4】:

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

        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)
        

        【讨论】:

          猜你喜欢
          • 2016-03-30
          • 1970-01-01
          • 1970-01-01
          • 2022-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多