import csv
from collections import Counter
from collections import defaultdict
from datetime import datetime

# Make dictionary with district as key

# Create the CSV file: csvfile
csvfile = open('crime_sampler.csv', 'r')

# Create a dictionary that defaults to a list: crimes_by_district
crimes_by_district = defaultdict(list)

# Loop over a DictReader of the CSV file
for row in csv.DictReader(csvfile):
    # Pop the district from each row: district
    district = row.pop('District')
    # Append the rest of the data to the list for proper district in crimes_by_district
    crimes_by_district[district].append(row)

# Number of arrests in each City District for each year

# Loop over the crimes_by_district using expansion as district and crimes
for district, crimes in crimes_by_district.items():
    # Print the district
    print(district)
    
    # Create an empty Counter object: year_count
    year_count = Counter()
    
    # Loop over the crimes:
    for crime in crimes:
        # If there was an arrest
        if crime['Arrest'] == 'true':
            # Convert the Date to a datetime and get the year
            year = datetime.strptime(crime['Date'], '%m/%d/%Y %I:%M:%S %p').year
            # Increment the Counter for the year
            year_count[year] += 1
            
    # Print the counter
    print(year_count)

# Insight: Looks like most arrests took place in the 11th district




import csv
from collections import Counter
from collections import defaultdict
from datetime import datetime

# Make dictionary with district as key

# Create the CSV file: csvfile
csvfile = open('crime_sampler.csv', 'r')

# Create a dictionary that defaults to a list: crimes_by_district
crimes_by_district = defaultdict(list)

# Loop over a DictReader of the CSV file
for row in csv.DictReader(csvfile):
    # Pop the district from each row: district
    district = row.pop('District')
    # Append the rest of the data to the list for proper district in crimes_by_district
    crimes_by_district[district].append(row)

# Number of arrests in each City District for each year

# Loop over the crimes_by_district using expansion as district and crimes
for district, crimes in crimes_by_district.items():
    # Print the district
    print(district)
    
    # Create an empty Counter object: year_count
    year_count = Counter()
    
    # Loop over the crimes:
    for crime in crimes:
        # If there was an arrest
        if crime['Arrest'] == 'true':
            # Convert the Date to a datetime and get the year
            year = datetime.strptime(crime['Date'], '%m/%d/%Y %I:%M:%S %p').year
            # Increment the Counter for the year
            year_count[year] += 1
            
    # Print the counter
    print(year_count)

# Insight: Looks like most arrests took place in the 11th district




# Make dictionary with district as key # Create the CSV file: csvfile # Create a dictionary that defaults to a list: crimes_by_district # Loop over a DictReader of the CSV file # Pop the district from each row: district # Append the rest of the data to the list for proper district in crimes_by_district # Number of arrests in each City District for each year # Loop over the crimes_by_district using expansion as district and crimes # Print the district # Create an empty Counter object: year_count # Loop over the crimes: # If there was an arrest # Convert the Date to a datetime and get the year # Increment the Counter for the year # Print the counter # Insight: Looks like most arrests took place in the 11th district