1
我正在使用python的Requests库从BestBuy Products API下载一些数据,并且我想将它们存储到pandas数据框中。将各种字段列表的JSON响应转换为Pandas数据框
类似的东西:
results = requests.get(url1,
params={'paramStuff'},
headers={'User-Agent': ua})
products = json.loads(results.text)
一个得到了很多的服务信息各领域的,因此我只瞄准,我想在JSON特定领域:
products['products']
我:
[{'details':[{'name': 'Name of Feature', 'value':'Value Of Feature'},
{'name': 'Name of Other Feature', 'value':'Value Of Other
Feature'}, ...],
'ProductId': 'Id Of Product 1',
'Some Other Field': 'Some Other Field Value'},
{same structure as above for other product}, {etc}]
因此,当你看到它就像是一个字典列表,其中又包含词典列表自己。要突出显示 - 细节词典可以有各种名称组合的名称:值(名称在产品中也不同)。
对如何处理这样的结构,以获得与这种格式的数据帧的任何想法:
+-----------+-------------------+-------------------+-------------------+------------------+
| ProductID | Name of Feature 1 | Name of Feature 2 | Name Of Feature 3 | Some Other Field |
+-----------+-------------------+-------------------+-------------------+------------------+
| Product 1 | Value | NULL | Value | Value |
| Product 2 | NULL | Value | Value | Value |
+-----------+-------------------+-------------------+-------------------+------------------+
到目前为止,我只设法得到这样的:
+-----------+-----------------------------------------------------------------------------------------------------------------------------------+------------------+
| ProductID | Details | Some Other Field |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------+------------------+
| Product 1 | [{'name': 'Name of Feature', 'value':'Value Of Feature'},{'name': 'Name of Other Feature', 'value':'Value Of Other Feature'},...] | Value 1 |
| Product 2 | [{'name': 'Name of Feature', 'value':'Value Of Feature'},{'name': 'Name of Other Feature', 'value':'Value Of Other Feature'},...] | Value 2 |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------+------------------+
您是否尝试过使用'pandas.read_json'? – jhamman
@jhamman是的,可悲的是它无法处理沉重的嵌套。我最终通过编写手动解析器来解决这个问题,如果没有更好的方法,我将在这里作为答案张贴 –