Skip to content

Instantly share code, notes, and snippets.

@b-ryan
Created February 25, 2018 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-ryan/6c789e5b9d3686bcfd17904c6e8c158d to your computer and use it in GitHub Desktop.
Save b-ryan/6c789e5b9d3686bcfd17904c6e8c158d to your computer and use it in GitHub Desktop.
gun data analysis
#!/usr/bin/env python
import csv
with open("gun-data.csv") as f:
r = csv.reader(f)
head = next(r)
i_date = head.index("Date")
i_deaths = head.index("Fatalities")
i_type = head.index("Type of weapons")
results = {
"1984-1994": {
"n_events": 0,
"n_deaths": 0,
},
"1994-2004": {
"n_events": 0,
"n_deaths": 0,
},
"2004-2014": {
"n_events": 0,
"n_deaths": 0,
},
}
n_events = 0
n_deaths = 0
for line in r:
weapons = line[i_type]
if "rifle" not in weapons.lower():
continue
date = line[i_date]
year = int(date.split("/")[2])
if year < 1900:
year = 2000 + year
if year > 2014 or year < 1984:
continue
if year > 2004:
bucket = "2004-2014"
elif year > 1994:
bucket = "1994-2004"
else:
bucket = "1984-1994"
results[bucket]["n_events"] += 1
results[bucket]["n_deaths"] += int(line[i_deaths])
print(results["1984-1994"])
print(results["1994-2004"])
print(results["2004-2014"])
@b-ryan
Copy link
Author

b-ryan commented Feb 25, 2018

results:

{'n_deaths': 78, 'n_events': 10}
{'n_deaths': 46, 'n_events': 7}
{'n_deaths': 78, 'n_events': 8}

@timvisher
Copy link

timvisher commented Feb 26, 2018

So I did my own quick analysis and I think that you may have been overly restrictive in your use of rifle as the predicate, since the Assault Weapons Ban in question also banned pistols and shotguns. My results are much more in keeping with the article. https://github.com/timvisher/gun-violence-analysis

$ lein run
{:bucket "2004-2014", :incidents 23, :deaths 226}
{:bucket "1994-2004", :incidents 8, :deaths 63}
{:bucket "1984-1994", :incidents 9, :deaths 104}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment