# Needed to display an image
from PIL import Image
# Needed to import the API data
import urllib
# Define the base url and the modifications, in this case just get a
# random image
= "https://cataas.com"
base_url = "/cat"
mods = base_url + mods
api_url
# Pull the image from the API
= urllib.request.urlopen(api_url)
api_data
# Display the image
open(api_data) Image.
CatAAS API
# Define the base url and the modifications, in this case just get a
# random image of a cat with specific features
= "https://cataas.com"
base_url = "/cat/orange,angry"
mods = base_url + mods
api_url
# Pull the image from the API
= urllib.request.urlopen(api_url)
api_data
# Display the image
open(api_data) Image.
# Define the base url and the modifications, in this case just get a
# random image with a given saying
= "https://cataas.com"
base_url = "/cat/says/Hello_DSC140"
mods = base_url + mods
api_url
# Pull the image from the API
= urllib.request.urlopen(api_url)
api_data
# Display the image
open(api_data) Image.
# Define the base url and the modifications, in this case just get a
# random image with a given saying and filters and formatted text
= "https://cataas.com"
base_url = "/cat/black/says/Hello_DSC140?fontSize=72&fontColor=red"
mods = base_url + mods
api_url
# Pull the image from the API
= urllib.request.urlopen(api_url)
api_data
# Display the image
open(api_data) Image.
# Define the base url and the modifications, in this case just get a
# random image cropped to a square, with negate filter
= "https://cataas.com"
base_url = "/cat?type=square&filter=negate"
mods = base_url + mods
api_url
# Pull the image from the API
= urllib.request.urlopen(api_url)
api_data
# Display the image
open(api_data) Image.
US Census Data
import json
import pandas as pd
# We specifically want to access thing housing value API
= 'https://api.census.gov/data/timeseries/eits/hv?' api
# These are the columns of data we want returned
= 'get=cell_value,time_slot_id,category_code,seasonally_adj,\
get data_type_code&geo_level_code&'
# Create the total URL
= api + get api_url
# This is a dictionary (a type of data structure) we can use to pass additional
# information to the API. Note that we are leaving api key blank but you can
# fill it in if you create one
= {}
params = 'from 2000 to 2010'
time 'time'] = time
params[= ''
api_key 'key'] = api_key params[
# Combine the url and the dict
# (we could just do this "by hand" but this is easier to read)
= api_url + urllib.parse.urlencode(params)
url print('Retrieving ', url)
Retrieving https://api.census.gov/data/timeseries/eits/hv?get=cell_value,time_slot_id,category_code,seasonally_adj,data_type_code&geo_level_code&time=from+2000+to+2010&key=
# Make the request and "decode" the data that is recieved
= urllib.request.urlopen(url)
api_data = api_data.read().decode() data
# Print the number of characters recieved
print('Retrieved',len(data),' characters')
Retrieved 198391 characters
# workflow: convert html-style data to a json object
# then to a pandas dataframe
= json.loads(data)
js = pd.DataFrame(js) census_data
# The first row in the dataframe are the column names
census_data
0 | 1 | 2 | 3 | 4 | 5 | 6 | |
---|---|---|---|---|---|---|---|
0 | cell_value | time_slot_id | category_code | seasonally_adj | data_type_code | geo_level_code | time |
1 | 3755 | 0 | ESTIMATE | no | SEASON | US | 2003-Q3 |
2 | 3773 | 0 | ESTIMATE | no | SEASON | US | 2003-Q4 |
3 | 611 | 0 | ESTIMATE | no | SEASON | WE | 2003-Q1 |
4 | 638 | 0 | ESTIMATE | no | SEASON | WE | 2003-Q2 |
... | ... | ... | ... | ... | ... | ... | ... |
3842 | 1048 | 0 | ESTIMATE | no | OCCUSE | SO | 2009-Q2 |
3843 | 1064 | 0 | ESTIMATE | no | OCCUSE | SO | 2009-Q3 |
3844 | 2133 | 0 | ESTIMATE | no | OCCUSE | US | 2009-Q1 |
3845 | 2009 | 0 | ESTIMATE | no | OCCUSE | US | 2009-Q2 |
3846 | 2071 | 0 | ESTIMATE | no | OCCUSE | US | 2009-Q3 |
3847 rows × 7 columns
# Make the column labels equal to the first row...
= census_data.iloc[0]
census_data.columns # ...then eliminate the first row
= census_data.drop(census_data.index[0])
census_data census_data
cell_value | time_slot_id | category_code | seasonally_adj | data_type_code | geo_level_code | time | |
---|---|---|---|---|---|---|---|
1 | 3755 | 0 | ESTIMATE | no | SEASON | US | 2003-Q3 |
2 | 3773 | 0 | ESTIMATE | no | SEASON | US | 2003-Q4 |
3 | 611 | 0 | ESTIMATE | no | SEASON | WE | 2003-Q1 |
4 | 638 | 0 | ESTIMATE | no | SEASON | WE | 2003-Q2 |
5 | 699 | 0 | ESTIMATE | no | SEASON | WE | 2003-Q3 |
... | ... | ... | ... | ... | ... | ... | ... |
3842 | 1048 | 0 | ESTIMATE | no | OCCUSE | SO | 2009-Q2 |
3843 | 1064 | 0 | ESTIMATE | no | OCCUSE | SO | 2009-Q3 |
3844 | 2133 | 0 | ESTIMATE | no | OCCUSE | US | 2009-Q1 |
3845 | 2009 | 0 | ESTIMATE | no | OCCUSE | US | 2009-Q2 |
3846 | 2071 | 0 | ESTIMATE | no | OCCUSE | US | 2009-Q3 |
3846 rows × 7 columns
# Find the 'TOTAL HOUSING UNITS' data in the entire US
= census_data[census_data["data_type_code"]=='TOTAL']
total_vals = total_vals[total_vals["geo_level_code"]=='US']
total_vals
# Find the 'OCCUPIED HOUSING UNITS' data in the entire US
= census_data[census_data["data_type_code"]=='OCC']
occupied_vals = occupied_vals[occupied_vals["geo_level_code"]=='US'] occupied_vals
import numpy as np
= np.array([float(i) for i in total_vals["cell_value"]])
total_units = np.array([float(i) for i in occupied_vals["cell_value"]])
occupied_units
= []
ratio for i in range(len(total_units)):
/total_units[i]*100) ratio.append(occupied_units[i]
# Now make a plot of the ratio
import matplotlib.pyplot as plt
plt.plot(ratio)'Quarter (2000-2010)')
plt.xlabel('Occupied Housing Units (%)') plt.ylabel(
Text(0, 0.5, 'Occupied Housing Units (%)')