Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am tring to get this code to work but get errors every time. I am trying to get it to create 2 files from

I am tring to get this code to work but get errors every time. I am trying to get it to create 2 files from the following data.

from collections import Counter

import csv import matplotlib.pyplot as plt import numpy as np

# Put the full path to your CSV/Excel file here MY_FILE = "c:\\TestData\\houston_crime_data_jan17_clearLake.csv"

def parse(raw_file, delimiter): """Parses a raw CSV file to a JSON-like object""" # Setup an empty list parsed_data = []

# Open CSV file, and safely close it when we're done with open(raw_file, newline='') as opened_file: # Read the CSV data csv_data = csv.reader(opened_file, delimiter=delimiter) # Skip over the first line of the file for the headers field_labels = next(csv_data) # Iterate over each row of the csv file, zip together field -> value for row in csv_data: parsed_data.append(dict(zip(field_labels, row)))

return parsed_data

def visualize_days(): """Visualize data by day of week""" data_file = parse(MY_FILE, ",") # Returns a dict where it sums the total values for each key. # In this case, the keys are the DaysOfWeek, and the values are # a count of incidents. counter = Counter(item["DayOfWeek"] for item in data_file)

# Separate out the counter to order it correctly when plotting. data_list = [ counter["Monday"], counter["Tuesday"], counter["Wednesday"], counter["Thursday"], counter["Friday"], counter["Saturday"], counter["Sunday"] ] day_tuple = tuple(["Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"])

# Assign the data to a plot plt.plot(data_list)

# Assign labels to the plot from day_list plt.xticks(range(len(day_tuple)), day_tuple)

# Save the graph! # If you look in your folder, you should see # the PNG file, "Days.png". This is our graph! plt.savefig("Days.png")

# Close figure plt.clf()

def visualize_type(): """Visualize data by category in a bar graph""" data_file = parse(MY_FILE, ",") # Same as before, this returns a dict where it sums the total # incidents per Category. counter = Counter(item["Category"] for item in data_file)

# Set the labels which are based on the keys of our counter. labels = tuple(counter.keys())

# Set where the labels hit the x-axis xlocations = np.arange(len(labels)) + 0.5

# Width of each bar width = 0.5

# Assign data to a bar plot plt.bar(xlocations, counter.values(), width=width)

# Assign labels and tick location to x-axis plt.xticks(xlocations + width / 2, labels, rotation=90)

# Give some more room so the labels aren't cut off in the graph plt.subplots_adjust(bottom=0.4)

# Make the overall graph/figure larger plt.rcParams['figure.figsize'] = 12, 8

# Save the graph! # If you look in your folder, you should see # the PNG file, "Type.png". This is our graph! plt.savefig("Type.png")

# Close figure plt.clf()

def main(): visualize_days() visualize_type()

if __name__ == "__main__": main()

And here is the data.

Date,Hour,Offense Type,Beat,Premise,BlockRange,StreetName,Type,Suffix,# offenses 1/22/2017,18,Theft,12D70,Department or Discount Store,100-199,EL DORADO,BLVD,W,1 1/22/2017,17,Auto Theft,12D70,Mall Parking Lot,500-599,BAYBROOK MALL,-,-,1 1/22/2017,17,Theft,12D70,Department or Discount Store,100-199,EL DORADO,BLVD,W,1 1/24/2017,18,Theft,12D70,Department or Discount Store,500-599,BAYBROOK MALL,-,-,1 1/25/2017,12,Theft,12D70,Miscellaneous Business (Non-Specific),1300-1399,BAY AREA,BLVD,-,1 1/25/2017,12,Theft,12D70,Apartment Parking Lot,15900-15999,GALVESTON,RD,-,1 1/25/2017,19,Theft,12D70,Car Wash,300-399,EL DORADO,BLVD,-,1 1/25/2017,7,Theft,12D70,Apartment Parking Lot,600-699,BARRINGER,LN,-,1 1/25/2017,5,Theft,12D70,Apartment Parking Lot,200-299,EL DORADO,BLVD,-,1 1/25/2017,1,Burglary,12D70,Apartment,15800-15899,GALVESTON,RD,-,1 1/28/2017,17,Theft,12D70,Department or Discount Store,1400-1499,BAY AREA,BLVD,W,1 1/28/2017,19,Theft,12D70,Mall Parking Lot,500-599,BAYBROOK MALL,-,-,1 1/28/2017,20,Auto Theft,12D70,Other Parking Lot,1500-1599,BAY AREA,BLVD,W,1 1/27/2017,14,Robbery,12D70,Jewelry Stores,500-599,BAYBROOK MALL,-,-,1 1/27/2017,10,Theft,12D70,Mall Common Area,500-599,BAYBROOK MALL,-,-,1 1/26/2017,11,Theft,12D70,Specialty Store (Non-Specific),500-599,EL DORADO,BLVD,-,1 1/26/2017,13,Theft,12D70,Clothing Store,1400-1499,BAY AREA,BLVD,W,1 1/26/2017,15,Theft,12D70,Department or Discount Store,600-699,BAYBROOK MALL,-,-,1 1/30/2017,5,Auto Theft,12D70,Other Parking Lot,18200-18299,GULF,FWY,-,1 1/30/2017,10,Theft,12D70,Miscellaneous Business (Non-Specific),15500-15599,GALVESTON,RD,-,1 1/30/2017,17,Theft,12D70,Grocery Store or Supermarket,100-199,EL DORADO,BLVD,W,1 1/30/2017,17,Theft,12D70,Department or Discount Store,200-299,BAYBROOK MALL,-,-,1 1/30/2017,17,Theft,12D70,Mall Common Area,500-599,BAYBROOK MALL,-,-,1 1/30/2017,23,Robbery,12D70,Apartment Parking Lot,400-499,EL DORADO,BLVD,-,1 1/11/2017,14,Theft,12D70,Department or Discount Store,18100-18199,GULF,FWY,-,1 1/11/2017,7,Auto Theft,12D70,Apartment Parking Lot,400-499,TRESVANT,DR,-,1 1/10/2017,15,Theft,12D70,Other Unknown or Not Listed,200-299,BAYBROOK MALL,-,-,1 1/10/2017,16,Theft,12D70,Jewelry Stores,500-599,BAYBROOK MALL,-,-,1 1/10/2017,20,Theft,12D70,Grocery Store or Supermarket Parking Lot,100-199,EL DORADO,BLVD,W,1 1/10/2017,20,Theft,12D70,Other Parking Lot,18100-18199,GULF,FWY,-,1 1/10/2017,19,Theft,12D70,Mall Common Area,500-599,BAYBROOK MALL,-,-,1 1/9/2017,5,Burglary,12D70,Commercial Building,1500-1599,BAY AREA,BLVD,W,1 1/9/2017,13,Theft,12D70,Mall Parking Lot,200-299,BAYBROOK MALL,-,-,1 1/9/2017,15,Robbery,12D70,Specialty Store (Non-Specific),15200-15299,GALVESTON,RD,-,1 1/9/2017,19,Theft,12D70,Restaurant or Cafeteria Parking Lot,1400-1499,BAY AREA,BLVD,W,1 1/9/2017,21,Theft,12D70,Mall Parking Lot,500-599,BAYBROOK MALL,-,-,1 1/6/2017,12,Auto Theft,12D70,Mall Parking Lot,500-599,BAYBROOK MALL,-,-,1 1/6/2017,14,Theft,12D70,Jewelry Stores,500-599,BAYBROOK MALL,-,-,1 1/5/2017,9,Theft,12D70,Mall Parking Lot,500-599,BAYBROOK MALL,-,-,1 1/5/2017,8,Auto Theft,12D70,Apartment Parking Lot,600-699,PINELOCH,DR,-,1 1/5/2017,15,Auto Theft,12D70,Apartment Parking Lot,500-599,EL DORADO,BLVD,-,1 1/5/2017,14,Theft,12D70,Mall Common Area,500-599,BAYBROOK MALL,-,-,1 1/8/2017,16,Theft,12D70,Grocery Store or Supermarket,100-199,EL DORADO,BLVD,W,1 1/8/2017,14,Theft,12D70,Apartment Parking Lot,500-599,EL DORADO,BLVD,-,1 1/8/2017,6,Burglary,12D70,Commercial Building,18300-18399,GULF,FWY,-,1 1/7/2017,20,Robbery,12D70,Mall Common Area,1000-1099,BAYBROOK MALL,-,-,1 1/7/2017,20,Theft,12D70,Mall Common Area,500-599,BAYBROOK MALL,-,-,1 1/7/2017,16,Theft,12D70,Department or Discount Store,200-299,BAYBROOK MALL,-,-,1 1/7/2017,12,Theft,12D70,Specialty Store (Non-Specific),1100-1199,BAYBROOK MALL,-,-,1 1/7/2017,15,Theft,12D70,Mall Common Area,500-599,BAYBROOK MALL,-,-,1 1/3/2017,15,Theft,12D70,Convenience Store,14800-14899,GALVESTON,RD,-,1 1/3/2017,13,Theft,12D70,Specialty Store (Non-Specific),700-799,BAYBROOK MALL,-,-,1 1/4/2017,20,Theft,12D70,Specialty Store (Non-Specific),500-599,BAYBROOK MALL,-,-,1 1/4/2017,14,Theft,12D70,Department or Discount Store,18100-18199,GULF,FWY,-,1 1/4/2017,15,Burglary,12D70,Residence or House,400-499,SEAFOAM,RD,-,1 1/4/2017,5,Theft,12D70,Vacant Grocery Store or Supermarket,100-199,EL DORADO,BLVD,W,1 1/4/2017,8,Theft,12D70,Apartment Parking Lot,600-699,BARRINGER,LN,-,1 1/2/2017,18,Robbery,12D70,Mall Parking Lot,500-599,BAYBROOK MALL,-,-,1 1/2/2016,11,Theft,12D70,Other Parking Lot,14500-14599,GALVESTON,RD,-,1 1/1/2017,7,Auto Theft,12D70,Apartment Parking Lot,200-299,EL DORADO,BLVD,-,1 1/1/2017,10,Theft,12D70,Other Parking Lot,100-199,EL DORADO,BLVD,W,1 1/1/2017,19,Auto Theft,12D70,Mall Parking Lot,600-699,BAYBROOK MALL,-,-,1 1/20/2017,14,Theft,12D70,Grocery Store or Supermarket,100-199,EL DORADO,BLVD,W,1 1/20/2017,12,Theft,12D70,Mall Parking Lot,700-799,BAYBROOK MALL,-,-,1 1/20/2017,13,Theft,12D70,Other Parking Lot,18600-18699,GULF,FWY,-,1 1/20/2017,13,Theft,12D70,Mall Common Area,500-599,BAYBROOK MALL,-,-,1 1/19/2017,17,Theft,12D70,Restaurant or Cafeteria,700-799,BAYBROOK MALL,-,-,1 1/19/2017,16,Theft,12D70,Mall Parking Lot,18600-18699,GULF,FWY,-,1 1/15/2017,9,Theft,12D70,Restaurant or Cafeteria Parking Lot,19000-19099,GULF,FWY,-,1 1/15/2017,11,Theft,12D70,Drug Store or Medical Supply,900-999,CLEAR LAKE CITY,BLVD,-,1 1/16/2017,18,Auto Theft,12D70,Mall Parking Lot,600-699,BAYBROOK MALL,-,-,1 1/16/2017,14,Aggravated Assault,12D70,Bar or Night Club Parking Lot,100-199,EL DORADO,BLVD,W,1 1/18/2017,18,Burglary,12D70,Apartment,200-299,EL DORADO,BLVD,-,1 1/18/2017,17,Theft,12D70,Restaurant or Cafeteria Parking Lot,18100-18199,GULF,FWY,-,1 1/18/2017,16,Theft,12D70,Other Parking Lot,18100-18199,GULF,FWY,-,1 1/17/2017,16,Theft,12D70,Apartment Parking Lot,15800-15899,GALVESTON,RD,-,1 1/18/2017,11,Theft,12D70,Department or Discount Store,100-199,EL DORADO,BLVD,W,1 1/14/2017,17,Theft,12D70,Mall Common Area,1100-1199,BAYBROOK MALL,-,-,1 1/14/2017,3,Aggravated Assault,12D70,Apartment,200-299,EL DORADO,BLVD,-,1 1/13/2017,19,Robbery,12D70,Other Unknown or Not Listed,200-299,EL DORADO,BLVD,-,1 1/12/2017,12,Theft,12D70,Apartment Parking Lot,600-699,BARRINGER,LN,-,1 1/12/2017,7,Theft,12D70,Apartment Parking Lot,600-699,BARRINGER,LN,-,1 1/12/2017,8,Burglary,12D70,Pawn Resale Shop or Flea Market,500-599,EL DORADO,BLVD,-,1 1/13/2017,8,Theft,12D70,Apartment Parking Lot,400-499,EL DORADO,BLVD,-,1 1/13/2017,13,Burglary,12D70,Apartment,15800-15899,GALVESTON,RD,-,1 1/12/2017,15,Theft,12D70,Gym Recreat Club House Indoor Pool Spa,14600-14699,GALVESTON,RD,-,1 11/12/2016,11,Theft,12D70,Auto Repair,18100-18199,GULF,FWY,-,1 11/10/2016,8,Robbery,12D70,Department or Discount Store,100-199,EL DORADO,BLVD,W,1 12/19/2016,11,Theft,12D70,Department or Discount Store,100-199,BAYBROOK MALL,-,-,1 12/30/2016,16,Theft,12D70,Strip Business Center Parking Lot,200-299,EL DORADO,BLVD,W,1 12/31/2016,1,Burglary,12D70,,19400-19499,GULF,FWY,-,1 12/31/2016,13,Burglary,12D70,Rental Storage Facility,15500-15599,GALVESTON,RD,-,1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions

Question

Psychological, financial, and career counseling.

Answered: 1 week ago