Add Image to Pandas Dataframe

Add Image to Pandas Dataframe

Sometimes we want to create a pandas dataframe that consists of images. We can achieve this task using the HTML module in Python Pandas.

We will convert the path of the image to an HTML tag. Then, convert the dataframe to an HTML table and display the image. Python has a built-in module called HTML. We can encode and decode HTML in Python.

Use HTML Module to Render Image in a Pandas DataFrame

In the following code, we have created a dataframe containing the information about the artist and the painting title. To display the images in this dataframe, we will make a list containing the paths of the images.

We will assign this list to the dataframe as a column. We have created a function that converts the path to the HTML’s img tag by concatenating <img src= with the path.

The function also resizes the image to a small scale using the width attribute. Then we have converted the pandas dataframe to an HTML table using the to_html() method.

We called the HTML method to render the images. Lastly, we displayed the whole dataframe using the display() method.

Example Code:

#Python 3.x
import pandas as pd
from IPython.core.display import HTML
df = pd.DataFrame([['Van Gogh', 'Starry Night'],
                   ['Van Gogh', 'Sunflowers'],
                   ['Van Gogh', 'Cafe Terrace at Night']],
columns = ['Artist', 'Painting Title'])
painting_images = [
def to_img_tag(path):
    return '<img src="'+ path + '" width="50" >'


add image in pandas dataframe

Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.


Related Article - Pandas DataFrame

  • Get Pandas DataFrame Column Headers as a List
  • Delete Pandas DataFrame Column
  • Convert Pandas Column to Datetime
  • Convert a Float to an Integer in Pandas DataFrame
  • Sort Pandas DataFrame by One Column's Values
  • Get the Aggregate of Pandas Group-By and Sum