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

Reg*_*ein 7 python ipython pandas jupyter-notebook

我有一个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'])
Run Code Online (Sandbox Code Playgroud)

但是当我显示框架时,每列只显示图像对象的to_string表示.

    Image
0   IPython.core.display.Image object
1   IPython.core.display.Image object
Run Code Online (Sandbox Code Playgroud)

这有什么解决方案吗?

谢谢你的帮助.

Fab*_*wig 10

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

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)))
Run Code Online (Sandbox Code Playgroud)


Reg*_*ein 9

我发现的解决方案是不使用IPython.display Image,而是使用IPython.display HTML和数据帧的to_html(escape = False)功能.

总而言之,它看起来像这样:

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

df = pd.DataFrame(['<img src="image01.png"/>', './image02.png'], columns = ['Image'])

HTML(df.to_html(escape=False))
Run Code Online (Sandbox Code Playgroud)