2

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.

4

2 回答 2

2

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()
于 2019-12-04T10:57:46.210 回答
1

没有任何回应,这是我想出的解决方案。我添加了一个列表理解来处理将每个系列放入数据框,然后作为此源的转置导致系列跨行而不是向下对齐。

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']
df = [pd.DataFrame(d[i]['observations']).loc[0] for i in d]
df = pd.DataFrame(df).T
于 2019-07-06T06:36:03.703 回答