首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python DataFrame问题

Python DataFrame问题
EN

Stack Overflow用户
提问于 2020-06-15 09:23:49
回答 1查看 60关注 0票数 0

我正在做一些网络抓取,并用抓取的数据制作csv。

我创建了一个包含列的数据帧:

代码语言:javascript
运行
复制
df = pd.DataFrame(columns=['name', 'type', 'regular_price', 'Image src', 'Attribute 1 name', 'Attribute 1 value(s)', 'Attribute 2 name', 'Attribute 2 value(s)', 'Attribute 1 visible', 'Attribute 1 global', 'Attribute 2 visible', 'Attribute 2 global'])

我循环遍历所有链接,逐页拉取数据,并将其附加到数据帧中,如下所示:

代码语言:javascript
运行
复制
df = df.append({'name':names,'type':'variable', 'regular_price':combprice, 'Image src':'1', 'Attribute 1 name':'Color' ,'Attribute 1 value(s)':'2' ,'Attribute 2 name':'Size' ,'Attribute 2 value(s)':sizes ,'Attribute 1 visible':'1' ,'Attribute 1 global':'1' ,'Attribute 2 visible':'1' ,'Attribute 2 global':'1'}, ignore_index=True)

除了'regular_price‘和'Image src’之外的所有列都在工作,我确实有'Attribute 1 value(s)‘在工作。尽管现在不是了。

在我执行的每个循环之后:

代码语言:javascript
运行
复制
df.at[count, 'Image src'] = images
df.at[count, 'Attribute 1 value(s)'] = colors

此外,'regular_price‘没有被填充的事实也很奇怪,因为我可以毫无问题地打印值,与’image‘列表一样

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2020-06-15 09:49:32

在迭代并在最后创建数据帧时,请尝试将值追加到字典中

代码语言:javascript
运行
复制
dicts = {}
keys = ['name', 'type', 'regular_price', 'Image src', 'Attribute 1 name', 'Attribute 1 value(s)', 'Attribute 2 name', 'Attribute 2 value(s)', 'Attribute 1 visible', 'Attribute 1 global', 'Attribute 2 visible', 'Attribute 2 global']

for i in keys:
        dicts[i] = names # and others

最后,您可以创建数据帧pd.DataFrame(dicts)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62380047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档