-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrimerates.py
73 lines (56 loc) · 1.64 KB
/
crimerates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from datetime import datetime
f = open("data/crimedata.csv", "r")
data = f.read()
# crime_data = []
new_list = []
rows = data.split('\n')
last_row_value = len(rows) - 1
data_split = rows[1:last_row_value]
# findings
# total crimes count
count = 0
# no location crimes count and list
no_location_list = []
no_location_count = len(no_location_list)
# crime with location
crime_with_location = []
for item in data_split:
rows_split = item.split(',')
crime_date = rows_split[1]
# date_clean = datetime.strptime(crime_date, "%Y-%m")
count += 1
crime_report_by = rows_split[2]
crime_long = rows_split[4]
crime_lat = rows_split[5]
crime_type = rows_split[9]
crime_outcome = rows_split[10]
# only float identified numbers
if crime_long != '':
crime_long = float(crime_long)
crime_lat = float(crime_lat)
crime_with_location.append([item])
else:
no_location_list.append([item])
crime = [crime_date, crime_report_by, crime_long, crime_lat, crime_type, crime_outcome]
new_list.append(crime)
# print(new_list[0: 4])
# use new_list to count how many violence occurs
crime_counts = {}
for item in new_list:
if item[4] in crime_counts:
crime_counts[item[4]] = crime_counts[item[4]] + 1
# print(item[4])
else:
crime_counts[item[4]] = 1
# print("no item")
# print(crime_counts)
# function crime counts
def count_crime_types(list):
if item[4] in list:
list[item[4]] = list[item[4]] + 1
return list
else:
list[item[4]] = 1
return list
crime_rates_numbers = count_crime_types(crime_counts)
print(crime_rates_numbers)