Skip to content Skip to sidebar Skip to footer

Pandas / Ipython Notebook: Include And Display An Image In A 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? I tried the following: import pandas as pd from

Solution 1:

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

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

Solution 2:

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.displayimportImage, HTML

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

HTML(df.to_html(escape=False))

Solution 3:

I think you are misunderstanding what gets stored in a dataframe and confusing it with how it's displayed. In order to show an image in the html table representation you'd have to write your own function to insert an image tag in the html table cell.

Post a Comment for "Pandas / Ipython Notebook: Include And Display An Image In A Dataframe"