python - Most efficient way of converting RESTful output to dataframe
问题描述
I have output from a REST call that I've converted to JSON.
It's a highly nested collection of dicts and lists, but I'm eventually able to convert it to dataframe as follows:
import panads as pd
from requests import get
url = 'http://stats.oecd.org/SDMX-JSON/data/MEI_FIN/IR3TIB.GBR+USA.M/all'
params = {
'startTime' : '2008-06',
'dimensionAtObservation' : 'TimeDimension'
}
r = get(url, params = params)
x = r.json()
d = x['dataSets'][0]['series']
a = pd.DataFrame(d['0:0:0']['observations'])
b = pd.DataFrame(d['0:1:0']['observations'])
This works absent some manipulation to make it easier to work with, and as there are multiple time series, I can do a version of the same for each, but it goes without saying it's kind of clunky.
Is there a better/cleaner way to do this.
解决方案
该pandasdmx
库使这个超级简单:
import pandasdmx as sdmx
df = sdmx.Request('OECD').data(
resource_id='MEI_FIN',
key='IR3TIB.GBR+USA.M',
params={'startTime': '2008-06', 'dimensionAtObservation': 'TimeDimension'},
).write()
推荐阅读
- c++ - 为什么 mdspan 访问器策略概念包含衰减成员?
- python-3.x - 如何在机器学习管道中添加一个自定义中间预处理器来处理 scikit-learn 中的 n-gram 列?
- php - 如何重写/重新格式化数组?
- r - 从列表 R 中提取 varest 对象
- apache-spark - 如何确定 Apache Livy 的“允许的最大活动会话数”?
- c - 问题:char d = -1 printf("%u",d); 为什么输出不是 255
- swift - 如何在 swift 5 中在 collectionviewcell 中显示视频并在不同的视图控制器中查看视频
- android - 如何在服务内收听 SMS Delivered 广播?
- c++ - 如何定义与使用 lambda 和捕获作为回调兼容的函数指针
- python - PCA 双图的解释