In [2]:
import pandas as pd
In [3]:
import json
from pandas.io.json import json_normalize
In [3]:
# define json string
data = [{'state': 'Florida',
'shortname': 'FL',
'info': {'governor': 'Rick Scott'},
'counties': [{'name': 'Dade', 'population': 12345},
{'name': 'Broward', 'population': 40000},
{'name': 'Palm Beach', 'population': 60000}]},
{'state': 'Ohio',
'shortname': 'OH',
'info': {'governor': 'John Kasich'},
'counties': [{'name': 'Summit', 'population': 1234},
{'name': 'Cuyahoga', 'population': 1337}]}]
In [4]:
# use normalization to create tables from nested element
json_normalize(data, 'counties')
Out[4]:
In [5]:
# further populate tables created from nested element
json_normalize(data, 'counties', ['state', 'shortname', ['info', 'governor']])
Out[5]:
In [4]:
# load json as string
json.load((open('data/world_bank_projects_less.json')))
Out[4]:
In [7]:
# load as Pandas dataframe
sample_json_df = pd.read_json('data/world_bank_projects_less.json')
sample_json_df
Out[7]:
Using data in file 'data/world_bank_projects.json' and the techniques demonstrated above,
In [ ]: