pandas /IPython Notebook:在数据框中包含并显示图像 [英] Pandas / IPython Notebook: Include and display an Image in a dataframe

查看:85
本文介绍了 pandas /IPython Notebook:在数据框中包含并显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pandas Dataframe,它也有一个带有图像文件名的列.如何在DataFrame内部显示图像?

I have a pandas Dataframe which also has a column with a filename of an image. How can I display the image inside of the 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表示形式.

But when I show the frame, each column only shows the to_string representation of the Image Object.

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

有什么解决办法吗?

感谢您的帮助.

推荐答案

我建议您使用格式化程序,而不是将html代码插入数据帧.不幸的是,您需要设置截断设置,以使长文本不会被"..."截断.

Instead of inserting the html code into the dataframe, I suggest to use a formatter. Unfortunately you need to set the truncation settings, so long text doesn't get truncated with "...".

import pandas as pd
from IPython.display import Image, 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=dict(Image=path_to_image_html)))

这篇关于 pandas /IPython Notebook:在数据框中包含并显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆