将图像添加到 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53468558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:10:28  来源:igfitidea点击:

Adding image to pandas DataFrame

pythonpandasbeautifulsoup

提问by Snedecor

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.

假设我有一个要导出为 PDF 的 DataFrame。在 DataFrame 中,我有以下列:代码、名称、价格、净值、销售额。每一行都是一个产品。

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.

我想向该 DataFrame 中的每个产品添加一个图像,我可以使用 BeautifulSoup 获得该图像。有什么方法可以将图像添加到 DataFrame吗?不是链接,只是产品的图片。

Being more specific i want something like this:

更具体地说,我想要这样的东西:

enter image description here

在此处输入图片说明

Code:

代码:

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

回答by chitown88

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

您可能需要稍微使用宽度和高度属性,但这应该会让您开始。基本上,您只是将图像/链接转换为 html,然后使用 df.to_html 来显示这些标签。请注意,它不会显示您是否在 Spyder 中工作,但是正如您在下面的输出中看到的那样,通过 jupyter notebooks 可以正常工作

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

enter image description here

在此处输入图片说明

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

然后,您可以选择在那里做什么才能转到 pdf。

You could save as html

你可以另存为 html

df.to_html('test_html.html', escape=False)

then simply use and html to pdf converter here, or use a library such as pdfkitor 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

然后在这里简单地使用 html 到 pdf 转换器,或者使用诸如pdfkitWeasyPrint 之类的库。我对这些并不完全熟悉(很久以前我只使用过其中一个),但这里有一个很好的链接

Good luck.

祝你好运。