[英]Python Pandas - Read JSON from list
我試圖從這個網站上的圖表中抓取數據: https : //www.spglobal.com/spdji/en/indices/equity/sp-bmv-ipc/#overview
我在圖表后面找到了 JSON 文件,並嘗試使用此代碼將其導入 Pandas:
import pandas as pd
url = "https://www.spglobal.com/spdji/en/util/redesign/index-data/get-performance-data-for-datawidget-redesign.dot?indexId=92330739&getchildindex=true&returntype=T-¤cycode=MXN¤cyChangeFlag=false&language_id=1"
with urllib.request.urlopen(url) as url:
data = json.loads(url.read().decode())
df = pd.DataFrame(data, columns=['indexLevelsHolder'])
Data=df.iloc[3 , 0]
通過這樣做,我得到了“數據”對象,它是一個包含 JSON 格式的時間序列數據的列表。
[{'effectiveDate': 1309406400000, 'indexId': 92330714, 'effectiveDateInEst': 1309392000000, 'indexValue': 43405.82, 'monthToDateFlag': 'N', 'quarterToDateFlag': 'N', 'yearToDateFlag': 'N', 'oneYearFlag': 'N', 'threeYearFlag': 'N', 'fiveYearFlag': 'N', 'tenYearFlag': 'Y', 'allYearFlag': 'Y', 'fetchedDate': 1626573344000, 'formattedEffectiveDate': '30-Jun-2011'}, .........
問題是我找不到讀取此 JSON 數據並獲取我需要的列(有效日期和索引值)的方法。
有什么辦法嗎? 謝謝
您可以使用pd.json_normalize
將 Json 加載到列中:
import json
import urllib
import pandas as pd
url = "https://www.spglobal.com/spdji/en/util/redesign/index-data/get-performance-data-for-datawidget-redesign.dot?indexId=92330739&getchildindex=true&returntype=T-¤cycode=MXN¤cyChangeFlag=false&language_id=1"
with urllib.request.urlopen(url) as url:
data = json.loads(url.read().decode())
df = pd.json_normalize(data["indexLevelsHolder"]["indexLevels"])
print(df)
印刷:
effectiveDate indexId effectiveDateInEst indexValue monthToDateFlag quarterToDateFlag yearToDateFlag oneYearFlag threeYearFlag fiveYearFlag tenYearFlag allYearFlag fetchedDate formattedEffectiveDate
0 1309406400000 92330714 1309392000000 43405.820000 N N N N N N Y Y 1626574897000 30-Jun-2011
1 1309492800000 92330714 1309478400000 43693.930000 N N N N N N Y Y 1626574897000 01-Jul-2011
2 1309752000000 92330714 1309737600000 43758.130000 N N N N N N Y Y 1626574897000 04-Jul-2011
3 1309838400000 92330714 1309824000000 43513.290000 N N N N N N Y Y 1626574897000 05-Jul-2011
...and son on.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.