【问题标题】:Convert Panda's dataframe which contains images to HTML将包含图像的 Panda 数据框转换为 HTML
【发布时间】:2021-05-07 14:24:39
【问题描述】:

下午好。

我目前在处理 Panda 的数据框时遇到问题。请考虑以下代码:

import pandas as pd
from IPython.core.display import display,HTML

df = pd.DataFrame([['A231', 'Book', 5, 3, 150], 
                   ['M441', 'Magic Staff', 10, 7, 200]],
                   columns = ['Code', 'Name', 'Price', 'Net', 'Sales'])

# your images
images = ['https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825',
          'https://i.pinimg.com/originals/d9/5c/9b/d95c9ba809aa9dd4cb519a225af40f2b.png'] 


df['image'] = images

# convert your links to html tags 
def path_to_image_html(path):
    return '<img src="'+ path + '" width="60" >'

pd.set_option('display.max_colwidth', None)

display(HTML(df.to_html(escape=False ,formatters=dict(image=path_to_image_html))))

其中显示以下内容:

如何将此特定数据框导入 HTML 文件?

我尝试了以下代码,

df.to_html(open('my_file2.html', 'w'))

效果很好,但是“my_file2.html”显示如下: 如何修改代码,以便在 my_file2.html 中的“图像”列中显示实际图像而不是图像的链接?

感谢您的关注!

【问题讨论】:

    标签: python pandas dataframe plot jupyter


    【解决方案1】:

    将其更改为 html 格式。所以图片是从源中挑选出来的。

    df1['image']=  '''<img src="''' + df1['image'] + '''">''' 
    with open('r.html', 'w') as fo:
        fo.write(df1.to_html(render_links=True,escape=False))
    

    【讨论】:

    • 非常感谢@simpleApp,周五愉快!
    猜你喜欢
    • 1970-01-01
    • 2016-09-12
    • 2014-05-25
    • 2021-05-02
    • 2019-09-30
    • 2019-04-21
    • 1970-01-01
    • 2011-06-02
    • 2010-10-14
    相关资源
    最近更新 更多