The dataset for Gun Deaths in US came from FiveThirtyEight.
It contains information on gun deaths in the US from 2012 to 2014, which can be read here.
*This Project covers topics like reading Data from CSV Module
and Datetime Module
.
Here, we will find Gun Deaths per Month, Gender, Ethnicity and Intent.*
import csv
data = list(csv.reader(open("guns.csv", "r")))
data[:5]
[['', 'year', 'month', 'intent', 'police', 'sex', 'age', 'race', 'hispanic', 'place', 'education'], ['1', '2012', '01', 'Suicide', '0', 'M', '34', 'Asian/Pacific Islander', '100', 'Home', '4'], ['2', '2012', '01', 'Suicide', '0', 'F', '21', 'White', '100', 'Street', '3'], ['3', '2012', '01', 'Suicide', '0', 'M', '60', 'White', '100', 'Other specified', '4'], ['4', '2012', '02', 'Suicide', '0', 'M', '64', 'White', '100', 'Home', '4']]
headers = data[0]
data = data[1:]
print(headers)
data[:5]
['', 'year', 'month', 'intent', 'police', 'sex', 'age', 'race', 'hispanic', 'place', 'education']
[['1', '2012', '01', 'Suicide', '0', 'M', '34', 'Asian/Pacific Islander', '100', 'Home', '4'], ['2', '2012', '01', 'Suicide', '0', 'F', '21', 'White', '100', 'Street', '3'], ['3', '2012', '01', 'Suicide', '0', 'M', '60', 'White', '100', 'Other specified', '4'], ['4', '2012', '02', 'Suicide', '0', 'M', '64', 'White', '100', 'Home', '4'], ['5', '2012', '02', 'Suicide', '0', 'M', '31', 'White', '100', 'Other specified', '2']]
year_counts = {}
for i in data:
years = i[1]
if years in year_counts:
year_counts[years] += 1
else:
year_counts[years] = 1
year_counts
{'2012': 33563, '2013': 33636, '2014': 33599}
import datetime
dates = [datetime.datetime(year = int(i[1]), month = int(i[2]), day = 1) for i in data]
# print(dates[:5])
date_counts = {}
for i in dates:
if i in date_counts:
date_counts[i] += 1
else:
date_counts[i] = 1
date_counts
{datetime.datetime(2012, 1, 1, 0, 0): 2758, datetime.datetime(2012, 2, 1, 0, 0): 2357, datetime.datetime(2012, 3, 1, 0, 0): 2743, datetime.datetime(2012, 4, 1, 0, 0): 2795, datetime.datetime(2012, 5, 1, 0, 0): 2999, datetime.datetime(2012, 6, 1, 0, 0): 2826, datetime.datetime(2012, 7, 1, 0, 0): 3026, datetime.datetime(2012, 8, 1, 0, 0): 2954, datetime.datetime(2012, 9, 1, 0, 0): 2852, datetime.datetime(2012, 10, 1, 0, 0): 2733, datetime.datetime(2012, 11, 1, 0, 0): 2729, datetime.datetime(2012, 12, 1, 0, 0): 2791, datetime.datetime(2013, 1, 1, 0, 0): 2864, datetime.datetime(2013, 2, 1, 0, 0): 2375, datetime.datetime(2013, 3, 1, 0, 0): 2862, datetime.datetime(2013, 4, 1, 0, 0): 2798, datetime.datetime(2013, 5, 1, 0, 0): 2806, datetime.datetime(2013, 6, 1, 0, 0): 2920, datetime.datetime(2013, 7, 1, 0, 0): 3079, datetime.datetime(2013, 8, 1, 0, 0): 2859, datetime.datetime(2013, 9, 1, 0, 0): 2742, datetime.datetime(2013, 10, 1, 0, 0): 2808, datetime.datetime(2013, 11, 1, 0, 0): 2758, datetime.datetime(2013, 12, 1, 0, 0): 2765, datetime.datetime(2014, 1, 1, 0, 0): 2651, datetime.datetime(2014, 2, 1, 0, 0): 2361, datetime.datetime(2014, 3, 1, 0, 0): 2684, datetime.datetime(2014, 4, 1, 0, 0): 2862, datetime.datetime(2014, 5, 1, 0, 0): 2864, datetime.datetime(2014, 6, 1, 0, 0): 2931, datetime.datetime(2014, 7, 1, 0, 0): 2884, datetime.datetime(2014, 8, 1, 0, 0): 2970, datetime.datetime(2014, 9, 1, 0, 0): 2914, datetime.datetime(2014, 10, 1, 0, 0): 2865, datetime.datetime(2014, 11, 1, 0, 0): 2756, datetime.datetime(2014, 12, 1, 0, 0): 2857}
sex_counts = {}
sex = [i[5] for i in data]
for i in sex:
if i in sex_counts:
sex_counts[i] += 1
else:
sex_counts[i] = 1
sex_counts
{'M': 86349, 'F': 14449}
race_counts = {}
race = [i[7] for i in data]
for i in race:
if i in race_counts:
race_counts[i] += 1
else:
race_counts[i] = 1
race_counts
{'Asian/Pacific Islander': 1326, 'White': 66237, 'Native American/Native Alaskan': 917, 'Black': 23296, 'Hispanic': 9022}
intent_counts = {}
intent_data = [i[3] for i in data]
for i in intent_data:
if i in intent_counts:
intent_counts[i] += 1
else:
intent_counts[i] = 1
intent_counts
{'Suicide': 63175, 'Undetermined': 807, 'Accidental': 1639, 'Homicide': 35176, 'NA': 1}
census = list(csv.reader(open("census.csv", "r")))
census
[['Id', 'Year', 'Id', 'Sex', 'Id', 'Hispanic Origin', 'Id', 'Id2', 'Geography', 'Total', 'Race Alone - White', 'Race Alone - Hispanic', 'Race Alone - Black or African American', 'Race Alone - American Indian and Alaska Native', 'Race Alone - Asian', 'Race Alone - Native Hawaiian and Other Pacific Islander', 'Two or More Races'], ['cen42010', 'April 1, 2010 Census', 'totsex', 'Both Sexes', 'tothisp', 'Total', '0100000US', '', 'United States', '308745538', '197318956', '44618105', '40250635', '3739506', '15159516', '674625', '6984195']]
mapping = {'Asian/Pacific Islander': int(census[1][14]) + int(census[1][15]),
'Black': int(census[1][12]),
'Hispanic': int(census[1][11]),
'Native American/Native Alaskan': int(census[1][13]),
'White': int(census[1][10])}
mapping
{'Asian/Pacific Islander': 15834141, 'Black': 40250635, 'Hispanic': 44618105, 'Native American/Native Alaskan': 3739506, 'White': 197318956}
race_per_hundredk = {}
for i in race_counts:
race_per_hundredk[i] = round((race_counts[i]/mapping[i])*100000, 2)
race_per_hundredk
{'Asian/Pacific Islander': 8.37, 'White': 33.57, 'Native American/Native Alaskan': 24.52, 'Black': 57.88, 'Hispanic': 20.22}
homicide_race_counts = {}
for i, race in enumerate(races):
if intent_data[i] == "Homicide":
if race in homicide_race_counts:
homicide_race_counts[race] += 1
else:
homicide_race_counts[race] = 1
for i in homicide_race_counts:
homicide_race_counts[i] = round((homicide_race_counts[i]/mapping[i]) * 100000, 2)
homicide_race_counts
{'White': 4.64, 'Asian/Pacific Islander': 3.53, 'Black': 48.47, 'Native American/Native Alaskan': 8.72, 'Hispanic': 12.63}