8

Pandas / IPython Notebook: Include and display an image in a datasframe

 2 years ago
source link: https://www.codesd.com/item/pandas-ipython-notebook-include-and-display-an-image-in-a-datasframe.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Pandas / IPython Notebook: Include and display an image in a datasframe

advertisements

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?

I tried the following:

import pandas as pd
from IPython.display import Image

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

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

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

Is there any solution for this?

Thanks for your help.


The solution I found is to not use the IPython.display Image, but to use the IPython.display HTML and the to_html(escape=False) feature of a dataframe.

Altogether, it looks like this:

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))


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK