Skip to content

Instantly share code, notes, and snippets.

@IT102Gists
Last active October 24, 2018 17:44
Show Gist options
  • Save IT102Gists/7f1c5fb4cb7ddccc99d0f5f44ee8c630 to your computer and use it in GitHub Desktop.
Save IT102Gists/7f1c5fb4cb7ddccc99d0f5f44ee8c630 to your computer and use it in GitHub Desktop.
Python CodeAlong: Use Python to analyze pet license data from the City of Seattle.
# pet license csv file available at:
# https://data.seattle.gov/Community/Seattle-Pet-Licenses/jguv-t9rb
# data provided by City of Seattle Department of Finance and
# Administrative Services
import csv
# row 0 = license date
# row 1 = license num
# row 2 = name
# row 3 = species
# row 4 = primary breed
# row 5 = secondary breed
# row 6 = zip code
def analyze_pet_names(filename):
"""reads csv file to analyze and count frequency of pet names"""
pet_names = {} # empty dict
with open(filename, "r", encoding="utf-8") as f:
csv_reader = csv.reader(f)
headers = next(csv_reader)
for row in csv_reader:
# ignores empty fields
if row[2] != "":
pet_names[row[2]] = pet_names.get(row[2], 0) + 1
# sort pet_names dict into a list of tuples by value
# practice with list comprehensions!
sorted_pet_names = [(k, pet_names[k])
for k in sorted(pet_names, key=pet_names.get, reverse=True)]
top_name = sorted_pet_names[0]
# fstrings require Python 3.6+
print(f"The most common pet name in Seattle is '{top_name[0]}' "\
f"which was listed on {top_name[1]} license applications.")
def analyze_pet_zips(filename):
"""reads csv file to analyze and count frequency of pet zip codes"""
pet_zips = {} # empty dict
with open(filename, "r", encoding="utf-8") as f:
csv_reader = csv.reader(f)
headers = next(csv_reader)
for row in csv_reader:
pet_zips[row[6]] = pet_zips.get(row[6], 0) + 1
# sort pet_zips dict into a list of tuples by value
# more practice with list comprehensions!
sorted_pet_zips = [(k, pet_zips[k])
for k in sorted(pet_zips, key=pet_zips.get, reverse=True)]
top_zip = sorted_pet_zips[0]
# fstrings require Python 3.6+
print(f"The most common pet zip code in Seattle is {top_zip[0]} "\
f"which was listed on {top_zip[1]} license applications.")
def analyze_pet_species(filename):
"""reads csv file to analyze and count frequency of pet species"""
pet_species = {} # empty dict
with open(filename, "r", encoding="utf-8") as f:
csv_reader = csv.reader(f)
headers = next(csv_reader)
for row in csv_reader:
pet_species[row[3]] = pet_species.get(row[3], 0) + 1
# sort pet_species dict into a list of tuples by value
# more practice with list comprehensions!
sorted_pet_species = [(k, pet_species[k])
for k in sorted(pet_species, key=pet_species.get, reverse=True)]
# fstrings require Python 3.6+
for species, count in sorted_pet_species:
print(f"{species} was listed on {count} license applications.")
def analyze_dog_breeds(filename):
"""reads csv file to analyze and count frequency primary breed for all dogs"""
dog_breeds = {} # empty dict
with open(filename, "r", encoding="utf-8") as f:
csv_reader = csv.reader(f)
headers = next(csv_reader)
for row in csv_reader:
dog_breeds[row[4]] = dog_breeds.get(row[4], 0) + 1
# sort dog_breeds dict into a list of tuples by value
# more practice with list comprehensions!
sorted_dog_breeds = [(k, dog_breeds[k])
for k in sorted(dog_breeds, key=dog_breeds.get, reverse=True)]
print("TOP 5 most popular dog breeds include:\n")
for breed, count in sorted_dog_breeds[0:5]:
print(breed, "=", count)
# output results to user
print("Seattle Pet License Summary\n".upper())
analyze_pet_zips("Seattle_Pet_Licenses.csv")
print("\n###\n")
analyze_pet_names("Seattle_Pet_Licenses.csv")
print("\n###\n")
analyze_pet_species("Seattle_Pet_Licenses.csv")
print("\n###\n")
analyze_dog_breeds("Seattle_Pet_Licenses.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment