0

Good afternoon.

I am currently having trouble with Panda's dataframe. Please consider the code below:

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 = ['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', None)

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

Which displays the following: Result

How can I import this specific dataframe to a HTML file?

I tried the following code,

df.to_html(open('my_file2.html', 'w'))

Which works fine, but, "my_file2.html" displays like this: my_file2.html How can I modify the code so that in my_file2.html, in the column "image", it shows the actual image and not the link to the image?

Thanks for your attention!

1 Answer 1

2

change it to html format. so image is picked from source.

df1['image']=  '''<img src="''' + df1['image'] + '''">''' 
with open('r.html', 'w') as fo:
    fo.write(df1.to_html(render_links=True,escape=False))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, @simpleApp, have a great Friday!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.