I have a df with pictures in it by following this stack overflow question. I want to style the df with some conditional formatting using pandas styler. However, for the images show up you need to turn the pandas df into an HTML df, and in order to style a df, you need to turn the df into a styler object. Is there any way to style a dataframe with images?
To get images
import pandas as pd
from IPython.core.display import HTML
df = pd.DataFrame([['A231', 'Book', 5, 3, 150],
['M441', 'Magic Staff', 10, 7, 200]],
columns = ['Code', 'Name', 'Price', 'Net', 'Sales'])
# your images
images = ['https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825',
'https://i.pinimg.com/originals/d9/5c/9b/d95c9ba809aa9dd4cb519a225af40f2b.png']
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', -1)
HTML(df.to_html(escape=False ,formatters=dict(image=path_to_image_html)))
to style
import seaborn as sns
cm = sns.light_palette("green", as_cmap=True)
s = df.style.background_gradient(cmap=cm)
s
I would really like to be able to style a dataframe with pictures, but I can't seem to figure out the object types. Any help is appreciated. Thanks!