Adding image to pandas DataFrame

Posted on

Question :

Adding image to pandas DataFrame

Suppose I have a DataFrame I want to export to a PDF. In the DataFrame I have the following columns: Code, Name, Price, Net, Sales. Every row is a Product.

I want to add to every product in that DataFrame an image which i could get using BeautifulSoup. Is there some way to add the image to the DataFrame? Not the link, just the image of the product.

Being more specific i want something like this:

enter image description here


import pandas as pd
df = pd.DataFrame([['A231', 'Book', 5, 3, 150], 
                   ['M441', 'Magic Staff', 10, 7, 200]],
                   columns = ['Code', 'Name', 'Price', 'Net', 'Sales')

#Suppose this are the links that contains the imagen i want to add to the DataFrame
images = ['Link 1','Link 2'] 
Asked By: Snedecor


Answer #1:

You’ll probably have to play a bit around with width and height attributes, but this should get you started. Basically, you’re just converting the image/links to html, then using the df.to_html to display those tags. Note, it won’t show if you’re working in Spyder, but as you can see below with my output, works fine through jupyter notebooks

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

df = pd.DataFrame([['A231', 'Book', 5, 3, 150], 
                   ['M441', 'Magic Staff', 10, 7, 200]],
                   columns = ['Code', 'Name', 'Price', 'Net', 'Sales'])

# your images
images = ['',

df['image'] = images

# convert your links to html tags 
def path_to_image_html(path):
    return '<img src="'+ path + '" width="60" >'

pd.set_option('display.max_colwidth', None)

display(HTML(df.to_html(escape=False ,formatters=dict(image=path_to_image_html))))

enter image description here

Then you have some options of what to do there to go to pdf.

You could save as html

df.to_html('test_html.html', escape=False, formatters=dict(image=path_to_image_html))

then simply use and html to pdf converter here, or use a library such as pdfkit or WeasyPrint. I’m not entirely familiar with those (I only used one of them once a long time ago), but here’s a good link

Good luck.

Answered By: Snedecor

Leave a Reply

Your email address will not be published. Required fields are marked *