Pandas / IPython Notebook:在数据框中包含并显示图像

2023-12-31

我有一个 pandas Dataframe,它还有一个带有图像文件名的列。如何在 DataFrame 中显示图像?

我尝试了以下方法:

import pandas as pd
from IPython.display import Image

df = pd.DataFrame(['./image01.png', './image02.png'], columns = ['Image'])

df['Image'] = Image(df['Image'])

但是当我显示框架时,每列仅显示图像对象的 to_string 表示形式。

    Image
0   IPython.core.display.Image object
1   IPython.core.display.Image object

有什么解决办法吗?

感谢您的帮助。


我建议使用格式化程序,而不是将 html 代码插入到数据框中。不幸的是,您需要设置截断设置,因此长文本不会被“...”截断。

import pandas as pd
from IPython.display import HTML

df = pd.DataFrame(['./image01.png', './image02.png'], columns=['Image'])

def path_to_image_html(path):
    return '<img src="'+ path + '"/>'

pd.set_option('display.max_colwidth', -1)

HTML(df.to_html(escape=False, formatters={'Image': path_to_image_html}))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pandas / IPython Notebook:在数据框中包含并显示图像 的相关文章

随机推荐