Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active February 8, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AO8/e179d0ac8708bbda877690e4897b1d79 to your computer and use it in GitHub Desktop.
Save AO8/e179d0ac8708bbda877690e4897b1d79 to your computer and use it in GitHub Desktop.
A quick program to analyze crime by reported address with Python 3 using local city crime data.
# Auburn, WA Police Data CSV available at: https://data.auburnwa.gov/
import csv
import sys
from collections import Counter
# CSV headers for reference
# [0] CASENUMBER
# [1] OFFENSE
# [2] OFFENSETYPE
# [3] OFFENSENAME
# [4] REPORTED
# [5] REPORTINGDISTRICT
# [6] Location
def main():
csv_file = "Crimes.csv"
total = get_total_crime(csv_file)
crime_locations = get_crime_location(csv_file)
print_results(total, crime_locations)
def get_total_crime(filename):
"""get total crimes from csv"""
total = 0
with open(filename, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
total += 1
return total
def get_crime_location(filename):
"""get address for each reported crime from csv"""
addresses = Counter()
with open(filename, "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
if row["Location"] != "":
# ['202 S DIVISION ST', 'Auburn, WA 98001', '(47.30555, -122.229817)']
items = row["Location"].split("\n")
address = (items[0], items[1])
addresses[address] += 1
return addresses
def print_results(total, crime_locations):
"""print crime by address summary"""
print(" CRIMES BY ADDRESS, 2017-PRESENT ".center(79, "-"))
print()
for address, count in crime_locations.most_common(10):
print(f"{address[0]}, {address[1]} = {count} crimes reported")
print(f"{round(count/total*100, 2)}% of crime in Auburn\n",
file=sys.stderr)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment