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是的,可悲的是它無法處理沉重的嵌套。我最終通過編寫手動解析器來解決這個問題,如果沒有更好的方法,我將在這裏作爲答案張貼 –