簡體   English   中英

如何將嵌套的 JSON 轉換為 Pandas 數據框?

[英]How to turn nested JSON into a Pandas Dataframe?

我知道這個問題已被問過很多次,但我似乎無法將任何解決方案應用於我的問題。

詢問:

import urllib, json
url = 'https://api.carbonintensity.org.uk/regional'
params = 'Accept: application/json'
r = requests.get(url = url, params = params) 
data = r.json()
print(data)

輸出:

{'data': [{'from': '2019-08-30T08:00Z', 'to': '2019-08-30T08:30Z', 'regions': [{'regionid': 1, 'dnoregion' : 'Scottish Hydro Electric Power Distribution', 'shortname': 'North Scotland', 'intensity': {'forecast': 32, 'index': 'very low'}, 'generationmix': [{'fuel': '生物量','perc':0},{'fuel':'coal','perc':0},{'fuel':'imports','perc':0},{'fuel':'gas' , 'perc': 8.3}, {'fuel': 'nuclear', 'perc': 0}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', ' perc': 12.8}, {'fuel': 'solar', 'perc': 0}, {'fuel': 'wind', 'perc': 78.9}]}, {'regionid': 2, 'dnoregion' : 'SP Distribution', 'shortname': 'South Scotland', 'intensity': {'forecast': 14, 'index': 'very low'}, 'generationmix': [{'fuel': 'biomass', 'perc': 1.6}, {'fuel': 'coal', 'perc': 0}, {'fuel': 'imports', 'perc': 1.9}, {'fuel': 'gas', 'perc ': 1.1}, {'fuel': 'nuclear', 'perc': 31.9}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 0.2}, {'fuel': 'solar', 'perc': 0.5}, {'fuel': 'wind', 'perc': 62.8}]}, {'regionid': 3, 'dnoregi on': 'Electricity North West', 'shortname': 'North West England', 'intensity': {'forecast': 53, 'index': 'very low'}, 'generationmix': [{'fuel': 'biomass', 'perc': 0}, {'fuel': 'coal', 'perc': 0}, {'fuel': 'imports', 'perc': 0}, {'fuel': 'gas ', 'perc': 13.3}, {'fuel': 'nuclear', 'perc': 49.4}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 0}, {'fuel': 'solar', 'perc': 0.3}, {'fuel': 'wind', 'perc': 37}]}, {'regionid': 4, 'dnoregion ': 'NPG North East', 'shortname': 'North East England', 'intensity': {'forecast': 25, 'index': 'very low'}, 'generationmix': [{'fuel': '生物量','perc':20.8},{'fuel':'coal','perc':0},{'fuel':'imports','perc':0},{'fuel':'gas' , 'perc': 0}, {'fuel': 'nuclear', 'perc': 71.3}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', ' perc': 0}, {'fuel': 'solar', 'perc': 1.3}, {'fuel': 'wind', 'perc': 6.6}]}, {'regionid': 5, 'dnoregion' : 'NPG Yorkshire', 'shortname': 'Yorkshire', 'intensity': {'forecast': 235, 'index': 'moderate'}, 'generationmix': [{' 燃料':'生物質','perc':39.3},{'fuel':'coal','perc':0},{'fuel':'imports','perc':0},{'fuel' : 'gas', 'perc': 47.9}, {'fuel': 'nuclear', 'perc': 0}, {'fuel': 'other', 'perc': 0}, {'fuel': ' Hydro', 'perc': 0}, {'fuel': 'solar', 'perc': 1.1}, {'fuel': 'wind', 'perc': 11.7}]}, {'regionid': 6 , 'dnoregion': 'SP Manweb', 'shortname': 'North Wales and Merseyside', 'intensity': {'forecast': 257, 'index': 'moderate'}, 'generationmix': [{'fuel' : 'biomass', 'perc': 0}, {'fuel': 'coal', 'perc': 6}, {'fuel': 'imports', 'perc': 27}, {'fuel': '氣體','perc':20.3},{'fuel':'核','perc':0},{'fuel':'other','perc':0},{'fuel':'hydro' , 'perc': 2}, {'fuel': 'solar', 'perc': 2.7}, {'fuel': 'wind', 'perc': 42}]}, {'regionid': 7, ' dnoregion':'WPD South Wales','shortname':'South Wales','intensity':{'forecast':282,'index':'high'},'generationmix':[{'fuel':'biomass ', 'perc': 0}, {'fuel': 'coal', 'perc': 0.1}, {'fuel': 'imports', 'perc': 0}, {'fuel': 'gas', 'perc':71.1},{'燃料':'n uclear', 'perc': 0}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 0}, {'fuel': 'solar' , 'perc': 6}, {'fuel': 'wind', 'perc': 22.8}]}, {'regionid': 8, 'dnoregion': 'WPD West Midlands', 'shortname': 'West Midlands ', 'intensity': {'forecast': 57, 'index': 'very low'}, 'generationmix': [{'fuel': 'biomass', 'perc': 1}, {'fuel': '煤炭','perc':0.4},{'fuel':'imports','perc':2.5},{'fuel':'gas','perc':10},{'fuel':'nuclear' , 'perc': 28.2}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 2.8}, {'fuel': 'solar', ' perc': 2.2}, {'fuel': 'wind', 'perc': 52.9}]}, {'regionid': 9, 'dnoregion': 'WPD East Midlands', 'shortname': 'East Midlands', 'intensity': {'forecast': 340, 'index': 'high'}, 'generationmix': [{'fuel': 'biomass', 'perc': 0}, {'fuel': 'coal', 'perc': 14.2}, {'fuel': 'imports', 'perc': 0}, {'fuel': 'gas', 'perc': 54}, {'fuel': 'nuclear', 'perc ': 0}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 0}, {'fuel': 'solar', 'perc': 8},{'燃料':'風','perc ': 23.8}]}, {'regionid': 10, 'dnoregion': 'UKPN East', 'shortname': 'East England', 'intensity': {'forecast': 125, 'index': 'low' }, 'generationmix': [{'fuel': 'biomass', 'perc': 0}, {'fuel': 'coal', 'perc': 0}, {'fuel': 'imports', 'perc ': 0}, {'fuel': 'gas', 'perc': 31.9}, {'fuel': 'nuclear', 'perc': 30.9}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 0}, {'fuel': 'solar', 'perc': 12.9}, {'fuel': 'wind', 'perc': 24.3} ]}, {'regionid': 11, 'dnoregion': 'WPD South West', 'shortname': 'South West England', 'intensity': {'forecast': 29, 'index': 'very low'} , 'generationmix': [{'fuel': 'biomass', 'perc': 0}, {'fuel': 'coal', 'perc': 0}, {'fuel': 'imports', 'perc' : 0}, {'fuel': 'gas', 'perc': 7.6}, {'fuel': 'nuclear', 'perc': 64}, {'fuel': 'other', 'perc': 0 }, {'fuel': 'hydro', 'perc': 0}, {'fuel': 'solar', 'perc': 25.3}, {'fuel': 'wind', 'perc': 3.1}] }, {'regionid': 12, 'dnoregion': 'SSE South', 'shortname': 'South England', 'intensity': {'forecast': 175, 'index': 'moderate'}, 'ge nerationmix': [{'fuel': 'biomass', 'perc': 5}, {'fuel': 'coal', 'perc': 0.4}, {'fuel': 'imports', 'perc': 0.8 }, {'fuel': 'gas', 'perc': 40.9}, {'fuel': 'nuclear', 'perc': 17.3}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 1.1}, {'fuel': 'solar', 'perc': 11.7}, {'fuel': 'wind', 'perc': 22.8}]}, {'regionid':13,'dnoregion':'UKPN London','shortname':'London','intensity':{'forecast':124,'index':'low'},'generationmix':[{ 'fuel': 'biomass', 'perc': 13.8}, {'fuel': 'coal', 'perc': 0.8}, {'fuel': 'imports', 'perc': 0.8}, {'fuel ': 'gas', 'perc': 24.2}, {'fuel': 'nuclear', 'perc': 22.4}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 1.5}, {'fuel': 'solar', 'perc': 3.7}, {'fuel': 'wind', 'perc': 32.8}]}, {'regionid': 14, 'dnoregion': 'UKPN South East', 'shortname': 'South East England', 'intensity': {'forecast': 213, 'index': 'moderate'}, 'generationmix': [{'fuel ': 'biomass', 'perc': 3.1}, {'fuel': 'coal', 'perc': 0.2}, {'fuel': 'imports', 'perc': 20.1}, {'fuel': ' 氣體', 'perc': 48.7}, {'fuel': 'nuclear', 'perc': 5.6}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro' , 'perc': 1.6}, {'fuel': 'solar', 'perc': 7.6}, {'fuel': 'wind', 'perc': 13.1}]}, {'regionid': 15, ' dnoregion': 'England', 'shortname': 'England', 'intensity': {'forecast': 160, 'index': 'moderate'}, 'generationmix': [{'fuel': 'biomass', ' perc': 9.2}, {'fuel': 'coal', 'perc': 2.3}, {'fuel': 'imports', 'perc': 2.4}, {'fuel': 'gas', 'perc' : 31.5}, {'fuel': 'nuclear', 'perc': 23.1}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 0.6 }, {'fuel': 'solar', 'perc': 6.8}, {'fuel': 'wind', 'perc': 24.1}]}, {'regionid': 16, 'dnoregion': '蘇格蘭' , 'shortname': 'Scotland', 'intensity': {'forecast': 23, 'index': 'very low'}, 'generationmix': [{'fuel': 'biomass', 'perc': 1.1} , {'fuel': 'coal', 'perc': 0}, {'fuel': 'imports', 'perc': 1.3}, {'fuel': 'gas', 'perc': 3.9}, { '燃料':'核','perc':21.9},{'fuel':'other','perc':0},{'fuel':'hydro','perc':4.4},{'fuel ':'太陽能','perc':0 .4},{'燃料':'風','perc':67}]},{'regionid':17,'dnoregion':'威爾士','shortname':'威爾士','強度':{ '預測':227,'指數':'中等'},'generationmix':[{'fuel':'biomass','perc':0},{'fuel':'coal','perc':0 }, {'fuel': 'imports', 'perc': 20.6}, {'fuel': 'gas', 'perc': 33.9}, {'fuel': 'nuclear', 'perc': 0}, {'fuel': 'other', 'perc': 0}, {'fuel': 'hydro', 'perc': 1.5}, {'fuel': 'solar', 'perc': 3.3}, {'燃料':'風','perc':40.7}]},{'regionid':18,'dnoregion':'GB','shortname':'GB','intensity':{'forecast':146, 'index': 'low'}, 'generationmix': [{'fuel': 'biomass', 'perc': 7.5}, {'fuel': 'coal', 'perc': 2.8}, {'fuel' :'進口','perc':6.4},{'fuel':'gas','perc':23.7},{'fuel':'nuclear','perc':20.5},{'fuel':'其他','perc':0.3},{'fuel':'hydro','perc':1.1},{'fuel':'solar','perc':7.5},{'fuel':'wind' , 'perc': 30.2}]}]}]}

我需要將此輸出拆分為 DataFrame 中的列,我嘗試了一些方法,但我對解析 JSON 缺乏經驗,絕對需要幫助! 我試過了:

json_normalize(data)

只返回包含所有數據的一行。

pd.io.json.json_normalize(data['data'])

返回 3 列('from', 'regions', 'to')而不解壓整個 json。

您始終可以通過使用對象的鍵訪問數據來解包數據

pd.io.json.json_normalize(data['data'][0]['from'])
pd.io.json.json_normalize(data['data'][0]['to'])
pd.io.json.json_normalize(data['data'][0]['regions'])

然后您可以以與以下類似的方式遍歷區域中的列表

for everyRegion in enumerate(data['data'][0]['regions']):
    pd.io.json.json_normalize(data['data'][0]['regions'][everyRegion])

然后創建一個數據框,然后將所需的值寫入數據框中的相應列。

請參閱此以了解有關 json 的更多信息 - JSON 編碼器和解碼器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM