【问题标题】:how to pass a pandas dataframe to html如何将熊猫数据框传递给html
【发布时间】:2021-07-14 22:05:19
【问题描述】:

我有一个示例数据框

city          city_website
LosAngeles     https://la.com
Ohio           https://ohio.com
Buffalo        https://buffalo.com
Manhattan      https://manhattan.com

如何将数据框传递给我的示例模板

<!DOCTYPE html>
<html>
<body>

<a href="CITIES URLs">CITIES NAMES</a>

</body>
</html>

不幸的是,我的 html 模板不接受直接数据框来显示内容。我试图将数据帧作为字符串传递给我的 html df = df.to_string()。但是导致

<html>
<body>
<p>Losangeles https://la.com Ohio https://ohio.com Buffalo https://buffalo.com Manhattan https://manhattan.com</p>
</body>
</html>

寻找想要的结果

<html>
<body>
<a href='https://la.com'>LA</a>
<br><a href='https://ohio.com'>Ohio</a>
<br><a href='https://buffalo.com'>Buffalo</a>
<br><a href='https://manhattan.com'>Manhattan</a>

</body>
</html>

【问题讨论】:

    标签: python html pandas frontend


    【解决方案1】:

    您可以使用df.to_html() 来获取一个 html 表格

    【讨论】:

    • 它没有给我超链接标签对吗?
    【解决方案2】:

    如果您无法让render_links 参数正常工作,您可以生成 html 字符串并将其传递给您的模板

    def city_link(row):
        return f"<a href='{row.city_website}'>{row.city}</a>"
    
    city_links = df.apply(city_link, axis=1)
    html = '<br>'.join(city_links.values)
    
    

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多