This Jupyter notebook shows the python library jsonstat.py in action. The JSON-stat is a simple lightweight JSON dissemination format. For more information about the format see the official site. This example shows how to explore the example data file oecd-canada from json-stat.org site. This file is compliant to the version 1 of jsonstat.
In [1]:
# all import here
from __future__ import print_function
import os
import pandas as ps # using panda to convert jsonstat dataset to pandas dataframe
import jsonstat # import jsonstat.py package
import matplotlib as plt # for plotting
In [2]:
%matplotlib inline
Download or use cached file oecd-canada.json. Caching file on disk permits to work off-line and to speed up the exploration of the data.
In [3]:
url = 'http://json-stat.org/samples/oecd-canada.json'
file_name = "oecd-canada.json"
file_path = os.path.abspath(os.path.join("..", "tests", "fixtures", "www.json-stat.org", file_name))
if os.path.exists(file_path):
print("using already downloaded file {}".format(file_path))
else:
print("download file and storing on disk")
jsonstat.download(url, file_name)
file_path = file_name
Initialize JsonStatCollection from the file and print the list of dataset contained into the collection.
In [4]:
collection = jsonstat.from_file(file_path)
collection
Out[4]:
Select the dataset named oedc
. Oecd dataset has three dimensions (concept, area, year), and contains 432 values.
In [5]:
oecd = collection.dataset('oecd')
oecd
Out[5]:
Shows some detailed info about dimensions
In [6]:
oecd.dimension('concept')
Out[6]:
In [7]:
oecd.dimension('area')
Out[7]:
In [8]:
oecd.dimension('year')
Out[8]:
In [9]:
oecd.data(area='IT', year='2012')
Out[9]:
In [10]:
oecd.value(area='IT', year='2012')
Out[10]:
In [11]:
oecd.value(concept='unemployment rate',area='Australia',year='2004') # 5.39663128
Out[11]:
In [12]:
oecd.value(concept='UNR',area='AU',year='2004')
Out[12]:
In [13]:
df_oecd = oecd.to_data_frame('year', content='id')
df_oecd.head()
Out[13]:
In [14]:
df_oecd['area'].describe() # area contains 36 values
Out[14]:
Extract a subset of data in a pandas dataframe from the jsonstat dataset. We can trasform dataset freezing the dimension area to a specific country (Canada)
In [15]:
df_oecd_ca = oecd.to_data_frame('year', content='id', blocked_dims={'area':'CA'})
df_oecd_ca.tail()
Out[15]:
In [16]:
df_oecd_ca['area'].describe() # area contains only one value (CA)
Out[16]:
In [17]:
df_oecd_ca.plot(grid=True)
Out[17]:
In [18]:
oecd.to_table()[:5]
Out[18]:
It is possible to trasform jsonstat data into table in different order
In [20]:
order = [i.did for i in oecd.dimensions()]
order = order[::-1] # reverse list
table = oecd.to_table(order=order)
table[:5]
Out[20]: