如何将字典附加到大 pandas 数据框? [英] How to append a dictionary to a pandas dataframe?

查看:149
本文介绍了如何将字典附加到大 pandas 数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组包含json文件的URL和一个空的熊猫数据框,其中列表示jsnon文件的属性。并不是所有的json文件都有大熊猫数据框中的所有属性。我需要做的是从json文件中创建字典,然后将每个字典作为新行附加到大熊猫数据框,如果json文件没有与数据帧中的列匹配的属性,则必须填写空白。



我设法创建字典:

  import urllib2 
import json

url =https://cws01.worldstores.co.uk/api/product.php?product_sku=ULST:7BIS01CF
data = urllib2。 urlopen(url).read()
data = json.loads(data)

然后我尝试创建一个for循环,如下所示:

  row = -1 
for i in links:
row = row + 1
data = urllib2.urlopen(str(i))。read()
data = json.loads(data)
for key.keys() :
df.columns中的列:
如果str(列)== str(key):
df.loc [[str(column)],row] = data [str键)]
else:
df.loc [[str(column)],row] = None

其中df是数据框,链接是一组url



但是,我收到以下错误:

  raise KeyError('%s not in index'%objarr [mask])

KeyError:['2_seater_depth_mm']不在索引

其中['2_seater_depth_mm']是大熊猫数据框的第一列

解决方案

对于我下面的代码作品:

  row = -1 
for i in links:
row = row + 1
data = urllib2.urlopen(str(i))。read()
data = json.loads(data)
for key.keys ():
df.loc [row,key] = data [key]

你在 .loc()中有一个参数的混合顺序,并且有一个多到 []


I have a set of urls containing json files and an empty pandas dataframe with columns representing the attributes of the jsnon files. Not all json files have all the attributes in the pandas dataframe. What I need to do is to create dictionaries out of the json files and then append each dictionary to the pandas dataframe as a new row and, in case the json file doesn't have an attribute matching a column in the dataframe this has to be filled blank.

I managed to create dictionaries as:

import urllib2
import json  

url = "https://cws01.worldstores.co.uk/api/product.php?product_sku=ULST:7BIS01CF"
data = urllib2.urlopen(url).read()
data = json.loads(data)

and then I tried to create a for loop as follows:

row = -1
for i in links:
    row = row + 1
    data = urllib2.urlopen(str(i)).read()
    data = json.loads(data)
    for key in data.keys():
        for column in df.columns:
            if str(column) == str(key):
                df.loc[[str(column)],row] = data[str(key)]
            else:
                df.loc[[str(column)],row] = None

where df is the dataframe and links is the set of urls

However, I get the following error:

raise KeyError('%s not in index' % objarr[mask])

KeyError: "['2_seater_depth_mm'] not in index"

where ['2_seater_depth_mm'] is the first column of the pandas dataframe

解决方案

For me below code works:

row = -1
for i in links:
    row = row + 1
    data = urllib2.urlopen(str(i)).read()
    data = json.loads(data)
    for key in data.keys():
        df.loc[row,key] = data[key]

You have mixed order of arguments in .loc() and have one to much []

这篇关于如何将字典附加到大 pandas 数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆