我正在使用 python 的 Requests 库从 BestBuy Products API 下载一些数据,我想将它们存储到 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}]
因此,如您所见,它类似于字典列表,而字典列表本身又包含字典列表。突出显示 - details dict 可以有各种名称组合列表:值(名称也因产品而异)。
关于如何处理这种结构以进入具有这种格式的数据框的任何想法:
+-----------+-------------------+-------------------+-------------------+------------------+
| 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 |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------+------------------+