Complex data structure in Python

result set from teradata result set


In [ ]:
import pprint
import json
from pandas.io.json import json_normalize

In [ ]:
ds = {'queryDuration': 47,
 'queueDuration': 213,
 'results': [{'data': [{'AREA_STATUS': 'ACTIVE', 'Count(*)': 33},
    {'AREA_STATUS': 'WDI1jHgkON', 'Count(*)': 1},
    {'AREA_STATUS': 'INACTIVE', 'Count(*)': 56}],
   'resultSet': True,
   'rowCount': 3,
   'rowLimitExceeded': False}]}

In [ ]:
type(ds)

In [ ]:
ds.to_json()

In [ ]:
ds

Little bit better (pretty) view of the data.


In [ ]:
pprint.pprint(ds)

In [ ]:
ds['queryDuration']

In [ ]:
ds['results']

In [ ]:
ds['results'][0]

In [ ]:
ds['results'][0]['data']

In [ ]:
pprint.pprint(ds['results'][0]['data'])

In [ ]:
pprint.pprint(ds['results'][0]['data'][0]['AREA_STATUS'])

Flatten out a big nested structure


In [ ]:
pprint.pprint(ds['results'][0]['data'])

In [ ]:
json_normalize(ds,['results','data'])

In [ ]:
ds_pd = json_normalize(ds,['results','data'],['queryDuration','queueDuration'])

In [ ]:
ds_pd

In [ ]:
ds_pd.describe()

In [ ]:


In [ ]:


In [ ]:


In [ ]: