Pandas / IPython Notebook: enable and display image in data frame

I have a pandas Dataframe that also has a column with a file name. How can I display an image inside a 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?

Thank you for your help.

+5
source share
3 answers

I found a solution not to use the IPython.display image, but to use the IPython.display HTML code and the to_html (escape = False) function in the data frame.

In general, 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)) 
+7
source

Instead of embedding html code in a data framework, I suggest using a formatter. Unfortunately, you need to set truncation settings, so long text will not be truncated "...".

 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))) 
+5
source

I think you misunderstand what is stored in the data framework and confuse it with how it is displayed. To show the image in the html table view, you will need to write your own function to insert the image tag into the html table cell.

+1
source

All Articles