联合国教科文组织SDMX api数据进入pandas DataFrame

时间:2018-02-04 07:47:47

标签: python dataframe sdmx

我正在尝试从联合国教科文组织数据api下载一个表格,并将其转换为pandas DataFrame。到目前为止,我到目前为止:

from pandasdmx import Request as rq
auth = {'Ocp-Apim-Subscription-Key': '75c17380c7204ff8996960054f60d261'}
uis = rq('unesco', headers = auth)

uis_resp = uis.get(resource_type = 'data',
               resource_id = 'RD',
               url = 'https://api.uis.unesco.org/sdmx//data/UNESCO,'
                   + 'RD'
                   + ',1.0/',                   
               params = {'key': '.CAP_PPP_CONST...........',
                         'format': 'sdmx-generic-2.1',
                         'subscription-key' : '75c17380c7204ff8996960054f60d261'})

uis_data = uis_resp.data
uis_iter = (s for s in uis_data.series) # Creating an iterator from the Series
data = uis_resp.write(list(uis_iter)) # Creating a dataframe

不幸的是,它给了我以下错误:

data = uis_resp.write(list(uis_iter)) # Creating a dataframe
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-db0f6c589a91> in <module>()
----> 1 data = uis_resp.write(list(uis_iter)) # Creating a dataframe

C:\Users\andras\Anaconda3\lib\site-packages\pandaSDMX-0.7.0-py3.6.egg\pandasdmx\api.py in write(self, source, **kwargs)
    633         if not source:
    634             source = self.msg
--> 635         return self._writer.write(source=source, **kwargs)
    636 
    637     def write_source(self, filename):

C:\Users\andras\Anaconda3\lib\site-packages\pandaSDMX-0.7.0-py3.6.egg\pandasdmx\writer\data2pandas.py in write(self, source, asframe, dtype, attributes, reverse_obs, fromfreq, parse_time)
    114                     pd_series, pd_attributes = zip(*series_list)
    115                 elif dtype:
--> 116                     key_fields = series_list[0].name._fields
    117                     pd_series = series_list
    118                 elif attributes:

IndexError: list index out of range

我做错了什么想法?

1 个答案:

答案 0 :(得分:1)

这很好用,pandasdmx的write()函数会自动创建一个Pandas DataFrame。

from pandasdmx import Request as rq

uis_key = '...'
auth = {'Ocp-Apim-Subscription-Key': uis_key}
uis = rq('UNESCO', headers = auth)

data = uis.data(
  resource_id='RD',
  key='.CAP_PPP_CONST...........',
  params={'subscription-key': uis_key}
).write()