### Install arcospy Package Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Install the arcospy package using pip. This command is essential for getting started with the library. ```python !pip install arcospy ``` -------------------------------- ### Install arcospy Source: https://github.com/jeffcsauer/arcospy/blob/master/README.md Install the arcospy package using pip. Ensure you have pandas and requests installed or they will be installed as dependencies. ```bash pip install arcospy ``` -------------------------------- ### Install and Import arcospy Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Install the arcospy package using pip and import it into your Python environment. This is a prerequisite for using the library. ```python !pip install arcospy import arcospy ``` -------------------------------- ### Get Pharmacy Latitude and Longitude Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Fetches the latitude and longitude for pharmacies within a specified county and state. Requires an API key. ```python arcospy.pharm_latlon(county = 'Hennepin', state = 'MN', key = 'WaPo') ``` -------------------------------- ### Plot Histogram of 'pills_per_person' Distribution Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Generates a histogram to visualize the distribution of the 'pills_per_person' column in the MN_merge DataFrame. This helps in identifying outliers and understanding the data spread. Requires matplotlib to be installed. ```python import matplotlib.pyplot as plt plt.style.use('ggplot') plt.hist(MN_merge['pills_per_person'], bins=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]) plt.show() ``` -------------------------------- ### Display arcospy Help Menu Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb View the available functions and their documentation within the arcospy package. This is useful for understanding the library's capabilities. ```python # Print the available functions help(arcospy) ``` -------------------------------- ### Initialize Empty DataFrame Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Initializes an empty pandas DataFrame to store pharmacy latitude and longitude data. ```python MN_pharm_latlon = pd.DataFrame() ``` -------------------------------- ### Plot Initial Pharmacy Locations Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Plots the initial pharmacy locations from the GeoDataFrame in red. ```python gdf.plot(color='red') ``` -------------------------------- ### Import arcospy Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Import the arcospy library into your Python environment. This makes all its functions available for use. ```python import arcospy ``` -------------------------------- ### Create Proportional Symbol Map Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Creates a map of Minnesota pharmacies, scaling point sizes by 'pills_per_person' and improving cartographic layout. Axes are turned off and a title is added. ```python fig,ax = plt.subplots(figsize=(16,10), subplot_kw={'aspect':'equal'}) plot = gdf.plot(markersize = 'pills_per_person', color = "red", ax=ax, ) plot.set_axis_off() plot.set_title("Pills per person per pharmacy in Minnesota (data source: ARCOS)") ``` -------------------------------- ### Display First Few Rows of Pharmacy Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Displays the first few rows of the MN_pharm_latlon DataFrame to show the fetched pharmacy location data. ```python MN_pharm_latlon.head() ``` -------------------------------- ### Load County Population Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Retrieves population data for counties within a given state. Supports multiple years and requires 'WaPo' key. ```python MN_county_pop = arcospy.county_population(state="MN", key="WaPo") MN_county_pop.tail() ``` -------------------------------- ### Retrieve Summarized Annual County Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Fetch summarized annual pill totals for a specific county and state using the summarized_county_annual function. Requires a valid API key. ```python arcospy.summarized_county_annual(county = 'Hennepin', state = 'MN', key = 'WaPo') ``` -------------------------------- ### Load Total Pharmacies for a State Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Fetches the total number of pharmacies within a specified state. Requires 'WaPo' key for authentication. ```python MN_pharm = arcospy.total_pharmacies_state(state='MN', key='WaPo') MN_pharm.head() ``` -------------------------------- ### Download Summarized County Annual Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Downloads summarized ARCOS data aggregated by county and year for a given state. Requires an API key. ```python # Download summarized data for Minnesota mn_summarized = arcospy.summarized_county_annual(state = 'MN', key = 'WaPo') mn_summarized.head() ``` -------------------------------- ### Plot Shapefile Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Generates a basic plot of the loaded shapefile to verify it renders correctly. This is a preliminary check before data merging. ```python mn_shp.plot() ``` -------------------------------- ### Import Matplotlib for Plotting Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Imports the matplotlib.pyplot module for advanced plotting functionalities. ```python import matplotlib.pyplot as plt ``` -------------------------------- ### Load Shapefiles with GeoPandas Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Loads a county shapefile into a GeoDataFrame. Ensure the file path is correct. ```python import geopandas as gpd mn_shp = gpd.read_file('C:/Users/jeffe/Desktop/example/tl_2010_27_county10.shp') ``` -------------------------------- ### Display First Few Rows of Merged Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Displays the first few rows of the MN_merge_latlon DataFrame after merging pharmacy location data. ```python MN_merge_latlon.head() ``` -------------------------------- ### Count Population Data Years Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Displays the distribution of population data across different years for a given state's counties. ```python MN_county_pop.year.value_counts() ``` -------------------------------- ### Merge Pharmacy and Population Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Combines pharmacy data with average county population data using pandas. Merging is done on the county name columns. ```python import pandas as pd MN_merge = pd.merge(MN_pharm, MN_county_pop_avg, left_on='buyer_county', right_on='BUYER_COUNTY') MN_merge.head() ``` -------------------------------- ### Display Data Types Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Shows the data types for each column in a pandas DataFrame. Useful for verifying data integrity after loading. ```python mn_summarized.dtypes ``` -------------------------------- ### Download Raw County Data with Arcospy Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Use the `county_raw` function to retrieve all drug transaction data for a specific county and state. This function provides detailed reporter-level information, which can result in a large dataset. ```python arcospy.county_raw(county = 'Lake', state = 'MN', key = 'WaPo') ``` -------------------------------- ### Plot Spatial Data with Color Map Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Visualizes the 'DOSAGE_UNIT' column from the merged spatial data on a map of Minnesota counties. Includes customization for the color bar and title. ```python import matplotlib.pyplot as plt # Define colors colormap = "plasma" # Draw the base percent population age 0-5 map ax = mn_2010_spatial.plot(column='DOSAGE_UNIT', cmap = colormap) ax.set_axis_off() ax.set_title("Total dosage units of major prescription opioids by county in MN in 2010 (data source: ARCOS)") fig = ax.get_figure() cbax = fig.add_axes([0.95, 0.3, 0.03, 0.39]) cbax.set_title('Total Dosage Units') sm = plt.cm.ScalarMappable(cmap=colormap, norm=plt.Normalize(vmin=min(mn_2010_spatial.DOSAGE_UNIT), vmax=max(mn_2010_spatial.DOSAGE_UNIT))) sm._A = [] fig.colorbar(sm, cax=cbax, format="%d") plt.show() ``` -------------------------------- ### Calculate Average Population per County Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Computes the average population for each county across all available years. This prepares the data for merging with pharmacy information. ```python MN_county_pop_avg = MN_county_pop.groupby('BUYER_COUNTY').mean().reset_index() MN_county_pop_avg.head() ``` -------------------------------- ### Fetch Pharmacy Lat/Lon by County Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Iterates through Minnesota counties to fetch pharmacy latitude and longitude data using arcospy.pharm_latlon. Appends results to a DataFrame, with error handling for any exceptions. ```python for i in MN_Counties: try: temp = arcospy.pharm_latlon(county = i, state = 'MN', key = 'WaPo') MN_pharm_latlon = MN_pharm_latlon.append(temp) except: pass ``` -------------------------------- ### Merge Pharmacy Data with Existing Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Merges the fetched pharmacy latitude and longitude data (MN_pharm_latlon) with an existing DataFrame (MN_merge) based on the buyer DEA number. ```python MN_merge_latlon = pd.merge(MN_merge, MN_pharm_latlon, left_on='buyer_dea_no', right_on='BUYER_DEA_NO') ``` -------------------------------- ### Create Scatter Plot Legend with Varying Marker Sizes Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Use this code to generate a legend for a scatter plot where marker size corresponds to data values. Ensure Matplotlib and NumPy are imported. ```python import numpy as np import matplotlib.pyplot as plt msizes = np.array([0, 1, 2, 3]) l1, = plt.plot([],[], 'or', markersize=msizes[0]) l2, = plt.plot([],[], 'or', markersize=msizes[1]) l3, = plt.plot([],[], 'or', markersize=msizes[2]) l4, = plt.plot([],[], 'or', markersize=msizes[3]) labels = ['0', '1', '10', '20'] leg = plt.legend([l1, l2, l3, l4], labels, ncol=1, frameon=True, fontsize=10, handlelength=1, loc = 1, borderpad = 0.5, handletextpad=1, title=' Avg. pills per person\n per pharmacy, 2006-2014', scatterpoints = 1) ``` -------------------------------- ### Calculate Pills Per Person Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Calculates the average number of pills per person per pharmacy over the study period. This metric normalizes drug dosage units by population and the number of years. ```python MN_merge['pills_per_person'] = MN_merge['total_dosage_unit']/MN_merge['population']/9 MN_merge.head() ``` -------------------------------- ### Identify Row with Maximum 'pills_per_person' Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Retrieves the entire row corresponding to the maximum value in the 'pills_per_person' column from the MN_merge DataFrame. This is useful for investigating extreme data points. ```python MN_merge.iloc[MN_merge['pills_per_person'].idxmax()] ``` -------------------------------- ### List of Minnesota Counties Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb A list of all Minnesota counties used for iterating through the pharm_latlon command. ```python MN_Counties = ['Aitkin', 'Anoka', 'Becker', 'Beltrami', 'Benton', 'Big Stone', 'Blue Earth', 'Brown', 'Carlton', 'Carver', 'Cass', 'Chippewa','Chisago','Clay','Clearwater','Cook','Cottonwood', 'Crow Wing','Dakota','Dodge','Douglas','Faribault','Fillmore','Freeborn','Goodhue','Grant', 'Hennepin','Houston','Hubbard','Isanti','Itasca','Jackson','Kanabec','Kandiyohi','Kittson', 'Koochiching','Lac Qui Parle','Lake','Lake Of The Wood','Le Sueur','Lincoln','Lyon','Mahnomen', 'Marshall','Martin','McLeod','Meeker','Mille Lacs','Morrison','Mower','Murray','Nicollet','Nobles', 'Norman','Olmsted','Otter Tail','Pennington','Pine','Pipestone','Polk','Pope','Ramsey','Red Lake', 'Redwood','Renville','Rice','Rock','Roseau','St. Louis','Scott','Sherburne','Sibley','Stearns', 'Steele','Stevens','Swift','Todd','Traverse','Wabasha','Wadena','Waseca','Washington','Watonwan', 'Wilkin','Winona','Wright','Yellow Medicine'] ``` -------------------------------- ### Filter Data by Year Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Filters a summarized dataset to include only records from a specific year. Assumes the input DataFrame 'mn_summarized' has a 'year' column. ```python mn_2010 = mn_summarized[mn_summarized['year']==2010] ``` -------------------------------- ### Filter Out-of-State Pharmacy Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Filters the GeoDataFrame to remove any pharmacy points with a latitude below 43, likely representing out-of-state data. ```python gdf = gdf[gdf['lat'] > 43] ``` -------------------------------- ### Merge Spatial and Tabular Data Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Merges a GeoDataFrame with a tabular DataFrame based on a common 'countyfips' column. This combines geographic information with ARCOS data. ```python mn_2010_spatial = mn_shp.merge(mn_2010, on='countyfips') ``` -------------------------------- ### Convert DataFrame to GeoDataFrame Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Getting up and running - examining pharmacy patterns.ipynb Converts the merged DataFrame (MN_merge_latlon) into a GeoDataFrame using latitude and longitude columns for geometry. ```python import geopandas gdf = geopandas.GeoDataFrame(MN_merge_latlon, geometry=geopandas.points_from_xy(MN_merge_latlon.lon, MN_merge_latlon.lat)) ``` -------------------------------- ### Rename GeoDataFrame Column Source: https://github.com/jeffcsauer/arcospy/blob/master/docs/Intro to arcospy - downloading data and making it spatial.ipynb Renames a column in the GeoDataFrame to match a column in another dataset for merging. This ensures compatibility for join operations. ```python mn_shp = mn_shp.rename(columns={"GEOID10": "countyfips"}) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.