# this will save the table to our local sqlite3 database - done already dfbig.to_sql('hpsa_mental_health_full', con = conn, if_exists = 'replace', index = False)
# dont run this twice baseurl = "https://api.usa.gov/crime/fbi/cde" API_KEY = {"API_KEY":"8rM3xn6vwWG6FEJCYYtgY2XKh8Kl47N08gx5ERx1"} for state in mhs_us_states: ''' Go through all states of interest and extract the crime estimate data from FBI records ''' urlvar="/estimate/state/%s"%state # estimate of crime in state date_from_to = {'from':'1979','to':'2022'} api_params = dict(**API_KEY,**date_from_to) # here I am using the unpacking operator ** to merge dictionaries response = requests.request("GET", baseurl+urlvar, params=api_params) crime_dat=response.json() est_crime=pd.DataFrame(crime_dat) if len(est_crime)==0: print("there was no data for %s"%state) else: fr_year=est_crime['year'].iloc[0] to_year=est_crime['year'].iloc[-1] print("data from %s actually in the range %i - %i"%(state,fr_year,to_year)) filename=('estimate_crime_%s_%s_%s.json'%(state,fr_year,to_year)) with open('./data/%s'%filename, 'w') as file: json.dump(crime_dat, file)
# Create an empty master dataframe master_hitlist_df=pd.DataFrame()
# saving to sqlite3 master_hitlist_df.to_sql('agency_hitlist', con = conn, if_exists = 'replace', index = False)# save to csv master_hitlist_df.to_csv('./tableau_datafiles/agency_hitlist.csv', index = False)
# turned this off now that table is present c.execute('DROP TABLE agency_data_extraction_sup_info;') # Save (commit) the changes: conn.commit()
# turned this off now that table is present - should save as .csv c.execute('CREATE TABLE agency_data_extraction_sup_info (id INTEGER PRIMARY KEY, mhsa_id INTEGER, agency_ori VCHAR, agency_name VCHAR, designate_year INTEGER, withdrawn_year INTEGER, data_from_year INTEGER, data_to_year INTEGER, filename VCHAR );') conn.commit()# this is for testing sup_data_entry = (None, 1, 'CA0150900', 'Tehachapi Police Department', 1999, 2005, 2003, 2022) # Execute Command c.execute('INSERT INTO agency_data_extraction_sup_info VALUES (?,?,?,?,?,?,?,?)',sup_data_entry) # Remember to commit the changes conn.commit()
# This loops ove the hits list found within certain raduis of shortage # the output is keeping tabs on the saved data to_year=2022 # this is to request the end for index, hit in master_hitlist_df.iterrows(): MHSA_idx=hit.iloc[0] designation_date=mh.iloc[MHSA_idx,:].Designation_Date withdrawn_date=mh.iloc[MHSA_idx,:].HPSA_Withdrawn_Date us_state=mh.iloc[MHSA_idx,:].State_Abbr city=mh.iloc[MHSA_idx,:].City county=mh.iloc[MHSA_idx,:].County_Name from_year=str(designation_date.split('-')[0]) withdrawn_year=str(withdrawn_date.split('-')[0]) df_arr,filename,dat_fr_year,dat_to_year = get_arrests(hit.ori,from_year,to_year,MHSA_idx) sup_data_entry = (None, MHSA_idx, hit.ori, hit.agency_name, from_year, withdrawn_year, dat_fr_year, dat_to_year,filename) print(sup_data_entry) c.execute('INSERT INTO agency_data_extraction_sup_info VALUES (?,?,?,?,?,?,?,?,?)',sup_data_entry) conn.commit()