首页 > 解决方案 > 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.

标签: pythonjsonpandassdmx

解决方案


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()

推荐阅读