def find_national_drugs(): getAll = drugs.get_reports() returnVal = { } for year in range(2002, 2015, 1): itemDict = {} itemDict['Sums'] = { } itemDict['Sums']['Marijuana'] = 0 itemDict['Sums']['Illicit Drugs'] = 0 itemDict['Sums']['Alcohol'] = 0 itemDict['Sums']['Population'] = 0 itemDict['Rates'] = { } itemDict['Rates']['Marijuana'] = 0 itemDict['Rates']['Illicit Drugs'] = 0 itemDict['Rates']['Alcohol'] = 0 returnVal[ str(year) ] = itemDict for item in getAll: itemDict = returnVal[ str(item['Year']) ] itemDict['Sums']['Marijuana'] += item['Totals']['Marijuana']['Used Past Year']['12-17']*10 itemDict['Sums']['Illicit Drugs'] += item['Totals']['Illicit Drugs']['Dependence Past Year']['12-17']*10 itemDict['Sums']['Alcohol'] += item['Totals']['Alcohol']['Dependence Past Year']['12-17']*10 itemDict['Sums']['Population'] += item['Population']['12-17'] for key in returnVal.keys(): itemDict = returnVal[key] itemDict['Rates']['Marijuana'] = round( itemDict['Sums']['Marijuana'] * 10000.0 / itemDict['Sums']['Population'] ) / 100 itemDict['Rates']['Illicit Drugs'] = round( itemDict['Sums']['Illicit Drugs'] * 10000.0 / itemDict['Sums']['Population'] ) / 100 itemDict['Rates']['Alcohol'] = round( itemDict['Sums']['Alcohol'] * 10000.0 / itemDict['Sums']['Population'] ) / 100 return returnVal
def find_state_drugs( code ): state = "" for stateName, cde in state_codes.items(): if cde == code: state = stateName getAll = drugs.get_reports() returnVal = { } for year in range(2002, 2015, 1): itemDict = {} returnVal[ str(year) ] = itemDict for item in getAll: if item['State'] == state: itemDict = returnVal[ str(item['Year']) ] itemDict['Rates'] = {} itemDict['Rates']['Marijuana'] = item['Rates']['Marijuana']['Used Past Year']['12-17'] itemDict['Rates']['Illicit Drugs'] = item['Rates']['Illicit Drugs']['Dependence Past Year']['12-17'] itemDict['Rates']['Alcohol'] = item['Rates']['Alcohol']['Dependence Past Year']['12-17'] return returnVal
'Washington': 'WA', 'North Carolina': 'NC', 'Maine': 'ME', 'New York': 'NY', 'Montana': 'MT', 'Nevada': 'NV', 'Delaware': 'DE', 'District of Columbia': 'DC' } def get_state_codes(): return state_codes #---------------------------DRUG STUFF------------------------------------ states_drugs = drugs.get_reports() def convert_drugs(): overall = {} for state in states_drugs: name = state_codes[state['State']] if not name in overall.keys(): overall[name] = {} weighted_data = [state['Totals']['Marijuana']['Used Past Year']['12-17']*1000, state['Totals']['Pain Relievers Abuse Past Year']['12-17']*1000, state['Totals']['Illicit Drugs']['Dependence Past Year']['12-17']*1000, state['Totals']['Alcohol']['Dependence Past Year']['12-17']*1000] overall[name][str(state['Year'])] = weighted_data return overall
import drugs import matplotlib.pyplot as plt import math import numpy as np list_of_report = drugs.get_reports() def makeAvg(list): sum = 0 for x in range(len(list)): sum += list[x] average = sum / len(list) return average mari = [] ageRange26mari = [] for n in list_of_report: ageRange26mari.append(n["Totals"]["Marijuana"]["Used Past Month"]["26+"]) avg1 = makeAvg(ageRange26mari) mari.append(avg1) ageRange1825mari = [] for n in list_of_report: ageRange1825mari.append( n["Totals"]["Marijuana"]["Used Past Month"]["18-25"]) avg2 = makeAvg(ageRange1825mari) mari.append(avg2) ageRange1217mari = []
import drugs import matplotlib.pyplot as plt plt.rcdefaults() import numpy as np import matplotlib.pyplot as plt listofreports = drugs.get_reports() # based on age group, how many people have been dependent on alcohol in the past year? for f in listofreports: oldest_group = f["Totals"]["Alcohol"]["Dependence Past Year"]["26+"] mid_group = f["Totals"]["Alcohol"]["Dependence Past Year"]["18-25"] young_group = f["Totals"]["Alcohol"]["Dependence Past Year"]["12-17"] objects = ['12-17 years old', '18-25 years old', '26+ years old'] x_pos = np.arange(len(objects)) values = [young_group, mid_group, oldest_group] clr = '#F09D00' labels = ['99', '400', '1200'] for i in range(len(x_pos)): plt.text(x=x_pos[i] - 0.1, y=values[i], s=labels[i], size=9) plt.bar(x_pos, values, align='center', color=clr) plt.xticks(x_pos, objects) plt.ylabel('# of people (in thousands)') plt.xlabel('age group') plt.title('Alcoholism in the Past Year') plt.show()
from course import corgis import drugs data = drugs.get_reports() data = corgis.data2frame(data) data.to_csv('drugs.csv', index=False)