Day 9

This is my work on Day 9.

Intro to JSON

In [1]:
import json
In [6]:
d = {55:"hello", 'foo':77.7, 'bar':None, 'up is down':False, 'mylist':[1,3,5]}
d
Out[6]:
{55: 'hello',
 'foo': 77.7,
 'bar': None,
 'up is down': False,
 'mylist': [1, 3, 5]}
In [7]:
json.dumps(d)
Out[7]:
'{"55": "hello", "foo": 77.7, "bar": null, "up is down": false, "mylist": [1, 3, 5]}'
In [8]:
with open('temp.json','w') as f:
    json.dump(d,f)
In [10]:
d2 = json.loads(json.dumps(d))
d2
Out[10]:
{'55': 'hello',
 'foo': 77.7,
 'bar': None,
 'up is down': False,
 'mylist': [1, 3, 5]}
In [11]:
d2.keys()
Out[11]:
dict_keys(['55', 'foo', 'bar', 'up is down', 'mylist'])

Escape from Jupyter

In [12]:
with open('f19_day09_class.ipynb') as f:
    j = json.load(f)
j.keys()
Out[12]:
dict_keys(['cells', 'metadata', 'nbformat', 'nbformat_minor'])
In [14]:
with open('temp.ipynb','w') as f:
    json.dump(j,f)
In [26]:
for cell in j['cells']:
    #print( cell['cell_type'] )
    #print( cell['source'] )
    print('# ' + '# '.join(cell['source']) )
    break 
# # Day 9
# This is my work on Day 9.

NHTSA Complaints Database

In [27]:
import requests
import json
url0 = 'http://www.nhtsa.gov/webapi/api/Complaints/vehicle/modelyear/{}/make/{}/model/{}?format=json'
year,make,model = '2005','Chevrolet','Cobalt'
url = url0.format(year,make,model)
s = requests.get(url).text  # a JSON string
complaints = json.loads(s)
In [28]:
type(complaints)
Out[28]:
dict
In [29]:
complaints.keys()
Out[29]:
dict_keys(['Count', 'Message', 'Results'])
In [30]:
complaints['Message']
Out[30]:
'Results returned successfully'
In [31]:
complaints['Count']
Out[31]:
1607
In [33]:
r = complaints['Results']
type(r),len(r)
Out[33]:
(list, 1607)
In [34]:
r[0]
Out[34]:
{'ODINumber': 10312200,
 'Manufacturer': 'General Motors LLC',
 'Crash': 'No',
 'Fire': 'No',
 'NumberOfInjured': 0,
 'NumberOfDeaths': 0,
 'DateofIncident': '/Date(1266296400000-0500)/',
 'DateComplaintFiled': '/Date(1266555600000-0500)/',
 'VIN': '1G1AL12F557',
 'Component': 'STEERING',
 'Summary': 'TL*THE CONTACT OWNS A 2005 CHEVROLET COBALT. THE CONTACT WAS DRIVING APPROXIMATELY 35 MPH ON NORMAL ROAD CONDITIONS.  SUDDENLY, THERE WAS A LOSS OF POWER STEERING.  THERE WAS DIFFICULTY STEERING THE VEHICLE WHICH RESULTED TO GREATER FORCE IN TURNING THE STEERING WHEEL. THE DRIVER PULLED OVER TO THE SIDE OF THE ROAD. THE ENGINE WAS TURNED OFF. THE VEHICLE RESTARTED AND RESUMED NORMAL OPERATION FOR A SHORT PERIOD TIME. THE FAILURE OCCURRED WHENEVER THE VEHICLE WAS IN OPERATION. CURRENTLY, THE VEHICLE IS INOPERATIVE.  THE VEHICLE HAS NOT BEEN REPAIRED FOR THE MALFUNCTION.  THE FAILURE MILEAGE WAS 89,000. ',
 'ProductType': 'VEHICLE',
 'ModelYear': '2005',
 'Make': 'CHEVROLET',
 'Model': 'COBALT'}
In [41]:
import requests
import json
url0 = 'http://www.nhtsa.gov/webapi/api/Complaints/vehicle/modelyear/{}/make/{}/model/{}?format=json'
make,model = 'Hyundai','Sonata'

d = {'count':[],'year':[]}
for year in range(2000,2019):
    url = url0.format(year,make,model)
    print(str(year)+'\r',end='')
    s = requests.get(url).text  # a JSON string
    complaints = json.loads(s)
    d['count'].append(complaints['Count'])
    d['year'].append(year)
2018
In [42]:
import altair as alt
alt.renderers.enable('notebook')
import pandas as pd
df = pd.DataFrame.from_dict(d)
alt.Chart(df).mark_bar().encode( x='year:O', y='count')
Out[42]:

Don't buy a 2011 Sonata!

In [ ]: