-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path2. importing_json_api.py
34 lines (26 loc) · 899 Bytes
/
2. importing_json_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# import pandas, numpy, json, pprint, and requests
import pandas as pd
import json
import pprint
import requests
pd.set_option('display.width', 200)
pd.set_option('display.max_columns', 8)
# load more complicated data
response = requests.get("https://openaccess-api.clevelandart.org/api/artworks/?african_american_artists")
camcollections = json.loads(response.text)
len(camcollections['data'])
pprint.pprint(camcollections['data'][0])
# flatten the data
camcollectionsdf = \
pd.json_normalize(camcollections['data'],
'citations',
['accession_number','title','creation_date',
'collection','creators','type'])
camcollectionsdf.head(2).T
creator = camcollectionsdf[:1].creators[0]
type(creator[0])
pprint.pprint(creator)
camcollectionsdf['birthyear'] = camcollectionsdf.\
creators.apply(lambda x: x[0]['birth_year'])
camcollectionsdf.birthyear.value_counts().\
sort_index().head()