数据来源:链接
import csv
import datetime
data = list(csv.reader(open("guns.csv")))[1:]
years_count = {}
years = [row[1]for row in data]
for item in years:
if item in years_count:
years_count[item] += 1
else:
years_count[item] = 1
dates = [datetime.datetime(year=int(row[1]),month=int(row[2]),day=1)for row in data]
date_counts= {}
for item in dates:
format_date = item.strftime("%Y-%m-%d")
if format_date in date_counts:
date_counts[format_date] += 1
else :
date_counts[format_date] = 1
def fac_counts(fac_num):
fac_counts ={}
for row in data:
if row[fac_num] in fac_counts:
fac_counts[row[fac_num]] += 1
else:
fac_counts[row[fac_num]] = 1
return fac_counts
sex_counts = fac_counts(5)
race_counts =fac_counts(7)
census =list(csv.reader(open("census.csv")))
race_population = {
"Asian/Pacific Islander":15159516+674625,
"Black":40250635,
"Native American/Native Alaskan":3739506,
"Hispanic":44618105,
"White":197318956
}
race_per_hundredk ={}
for item in race_counts:
race_per_hundredk[item] = race_counts[item]/race_population [item]*100000
intents =[row[3]for row in data]
races = [row[7]for row in data]
homicide_race_per_hundredk={}
for i, race in enumerate(races):
if intents[i] == "Homicide":
if race in homicide_race_per_hundredk:
homicide_race_per_hundredk[race] += 1
else:
homicide_race_per_hundredk[race] = 1
for item in homicide_race_per_hundredk:
homicide_race_per_hundredk[item] = homicide_race_per_hundredk[item]/race_population [item]*100000
homicide_race_per_hundredk