CatAAS API

# 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
base_url = "https://cataas.com"
mods = "/cat"
api_url = base_url + mods

# Pull the image from the API
api_data = urllib.request.urlopen(api_url)

# Display the image
Image.open(api_data)

# Define the base url and the modifications, in this case just get a 
# random image of a cat with specific features
base_url = "https://cataas.com"
mods = "/cat/orange,angry"
api_url = base_url + mods

# Pull the image from the API
api_data = urllib.request.urlopen(api_url)

# Display the image
Image.open(api_data)

# Define the base url and the modifications, in this case just get a 
# random image with a given saying
base_url = "https://cataas.com"
mods = "/cat/says/Hello_DSC140"
api_url = base_url + mods

# Pull the image from the API
api_data = urllib.request.urlopen(api_url)

# Display the image
Image.open(api_data)

# Define the base url and the modifications, in this case just get a 
# random image with a given saying and filters and formatted text
base_url = "https://cataas.com"
mods = "/cat/black/says/Hello_DSC140?fontSize=72&fontColor=red"
api_url = base_url + mods

# Pull the image from the API
api_data = urllib.request.urlopen(api_url)

# Display the image
Image.open(api_data)

# Define the base url and the modifications, in this case just get a 
# random image cropped to a square, with negate filter
base_url = "https://cataas.com"
mods = "/cat?type=square&filter=negate"
api_url = base_url + mods

# Pull the image from the API
api_data = urllib.request.urlopen(api_url)

# Display the image
Image.open(api_data)

US Census Data

import json
import pandas as pd
# We specifically want to access thing housing value API
api = 'https://api.census.gov/data/timeseries/eits/hv?'
# These are the columns of data we want returned
get = 'get=cell_value,time_slot_id,category_code,seasonally_adj,\
data_type_code&geo_level_code&'
# Create the total URL
api_url = api + get
# 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 = {}
time = 'from 2000 to 2010'
params['time'] = time
api_key = ''
params['key'] = api_key
# Combine the url and the dict
# (we could just do this "by hand" but this is easier to read)
url = api_url + urllib.parse.urlencode(params)
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
api_data = urllib.request.urlopen(url)
data = api_data.read().decode()
# 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
js = json.loads(data)
census_data = pd.DataFrame(js)
# 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.columns = census_data.iloc[0]
# ...then eliminate the first row
census_data = census_data.drop(census_data.index[0])
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
total_vals = census_data[census_data["data_type_code"]=='TOTAL']
total_vals = total_vals[total_vals["geo_level_code"]=='US']

# Find the 'OCCUPIED HOUSING UNITS' data in the entire US
occupied_vals = census_data[census_data["data_type_code"]=='OCC']
occupied_vals = occupied_vals[occupied_vals["geo_level_code"]=='US']
import numpy as np
total_units = np.array([float(i) for i in total_vals["cell_value"]])
occupied_units = np.array([float(i) for i in occupied_vals["cell_value"]])

ratio = []
for i in range(len(total_units)):
    ratio.append(occupied_units[i]/total_units[i]*100)
# Now make a plot of the ratio
import matplotlib.pyplot as plt

plt.plot(ratio)
plt.xlabel('Quarter (2000-2010)')
plt.ylabel('Occupied Housing Units (%)')
Text(0, 0.5, 'Occupied Housing Units (%)')