<jupyter_start><jupyter_text>Education Desert maps10, 20, 50 mi radii around higher education institutions. *What parts of the country are left out?*<jupyter_code>import pandas as pd
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
from pathlib import Path

from tools import tree
from datetime import datetime as dt
today = dt.today().strftime("%d-%b-%y")

today
RAW_DATA = Path("../data/raw/")
INTERIM_DATA = Path("../data/interim/")
PROCESSED_DATA = Path("../data/processed/")
FINAL_DATA = Path("../data/final/")
EXTERNAL_DATA = Path("../data/external/")
tree(PROCESSED_DATA)
county_data = pd.read_csv(PROCESSED_DATA / 'counties.csv')
county_data.head().T
institutions_data = pd.read_csv(PROCESSED_DATA / 'institutional_characteristics.csv')
institutions_data.head().T
county_shapes = gpd.read_file(PROCESSED_DATA / 'geodata' / 'tl_2019_us_county.shp')
county_shapes.head().T
import us
contiguous_fips = [state.fips for state in us.STATES_CONTIGUOUS]
mask_contiguous_fips = county_shapes['STATEFP'].isin(contiguous_fips)
county_shapes = county_shapes[mask_contiguous_fips]
contiguous_states = [state.name for state in us.STATES_CONTIGUOUS]
mask_contiguous_states = institutions_data['fips_state_code'].isin(contiguous_states)
institutions_data = institutions_data[mask_contiguous_states]
geo_institutions = gpd.GeoDataFrame(institutions_data, geometry=gpd.points_from_xy(institutions_data['longitude'], institutions_data['latitude']))
geo_institutions.crs
county_shapes.crs
geo_institutions.crs = county_shapes.crs
geo_institutions.plot();<jupyter_output><empty_output><jupyter_text>[EPSG: 4269](https://epsg.io/4269) uses degrees as its units of measure, we need to change that to meters so we can create _buffers_ of 10, 25, 50 miles around each institution.[EPSG: 3857](https://epsg.io/3857)<jupyter_code>geo_institutions_in_meters = geo_institutions.to_crs(epsg=3857)
geo_institutions_in_meters.head()
miles = 1609 # meters
institutions_10mi_radius = geo_institutions_in_meters.copy()
institutions_10mi_radius['geometry'] = institutions_10mi_radius['geometry'].buffer(10 * miles)
institutions_25mi_radius = geo_institutions_in_meters.copy()
institutions_25mi_radius['geometry'] = institutions_10mi_radius['geometry'].buffer(25 * miles)
institutions_50mi_radius = geo_institutions_in_meters.copy()
institutions_50mi_radius['geometry'] = institutions_50mi_radius['geometry'].buffer(50 * miles)
institutions_10mi_radius.to_crs(epsg=4269, inplace=True)
institutions_25mi_radius.to_crs(epsg=4269, inplace=True)
institutions_50mi_radius.to_crs(epsg=4269, inplace=True)
institutions_10mi_radius.plot();
institutions_25mi_radius.plot();
institutions_50mi_radius.plot();<jupyter_output><empty_output><jupyter_text>https://geopandas.org/set_operations.html<jupyter_code>gpd.overlay(county_shapes, institutions_10mi_radius, how = 'difference').plot();
gpd.overlay(county_shapes, institutions_25mi_radius, how = 'difference').plot();
gpd.overlay(county_shapes, institutions_50mi_radius, how = 'difference').plot();<jupyter_output><empty_output><jupyter_text>but overlay actually returns a geodataframe which we can use with geoplot!<jupyter_code>gpd.overlay(county_shapes, institutions_50mi_radius, how = 'difference')<jupyter_output><empty_output><jupyter_text>TODOattach county data, share underrepresented, hh income, median age, to these dataframes.  Data preparation<jupyter_code>county_data.head()
county_data['share_underrepresented'] = (county_data['black_alone'] + 
    county_data['american_indian_and_alaska_native'] + 
    county_data['native_hawaiian_and_pacific_islander'] + 
    county_data['latino_alone']) / county_data['universe']
county_data['geoid'] = county_data['geoid'].astype(str).str.zfill(5)

county_data = county_data[['geoid', 'name', 'universe', 'share_underrepresented']]
county_data.set_index('geoid', inplace = True)
median_age = pd.read_csv(EXTERNAL_DATA / 'processed' / 'acs5_2018_medianage_counties.csv')
median_hh_income = pd.read_csv(EXTERNAL_DATA / 'processed' / 'acs5_2018_medianhouseholdincome_counties.csv')
median_age.head()
median_hh_income.head()
# fix geoid
median_age['geoid'] = median_age['geoid'].astype(str).str.zfill(5)
median_hh_income['geoid'] = median_hh_income['geoid'].astype(str).str.zfill(5)
median_age = median_age[['geoid', 'median', 'male', 'female']]
median_age.columns = ['geoid', 'median_age', 'median_age_male', 'median_age_female']
median_age.set_index('geoid', inplace = True)
median_hh_income = median_hh_income[['geoid', 'median']]
median_hh_income.columns = ['geoid', 'median_hh_income']
median_hh_income.set_index('geoid', inplace=True)
county_shapes = county_shapes[['GEOID', 'geometry']]
county_shapes.set_index("GEOID", inplace = True)
working_gdf = county_shapes.join(county_data).join(median_age).join(median_hh_income)

working_gdf.head()
ten_miles = gpd.overlay(working_gdf, institutions_10mi_radius, how = 'difference')
twentyfive_miles = gpd.overlay(working_gdf, institutions_25mi_radius, how = 'difference')
fifty_miles = gpd.overlay(working_gdf, institutions_50mi_radius, how = 'difference')
ten_miles_inter = gpd.overlay(working_gdf, institutions_10mi_radius, how = 'intersection')
twentyfive_miles_inter = gpd.overlay(working_gdf, institutions_25mi_radius, how = 'intersection')
fifty_miles_inter = gpd.overlay(working_gdf, institutions_50mi_radius, how = 'intersection')
gplt.choropleth(ten_miles, projection=gcrs.WebMercator(), hue = 'share_underrepresented', figsize=(12,12),)
import matplotlib.colors as mc
univ_norm = mc.Normalize(ten_miles['universe'].min(), ten_miles['universe'].quantile(0.90))
proj = gcrs.AlbersEqualArea()

ax = gplt.polyplot(
    county_shapes,     
    zorder=-1,
    projection = proj,
    linewidth=1,
    edgecolor='white',
    facecolor='lightgray',
    figsize=(12, 12),
)
gplt.choropleth(ten_miles, hue = 'share_underrepresented', ax = ax)
proj = gcrs.AlbersEqualArea()

ax = gplt.polyplot(
    county_shapes,     
    zorder=-1,
    projection = proj,
    linewidth=1,
    edgecolor='white',
    facecolor='lightgray',
    figsize=(12, 12),
)
gplt.choropleth(twentyfive_miles, hue = 'share_underrepresented', ax = ax)
proj = gcrs.AlbersEqualArea()

ax = gplt.polyplot(
    county_shapes,     
    zorder=-1,
    projection = proj,
    linewidth=1,
    edgecolor='white',
    facecolor='lightgray',
    figsize=(12, 12),
)
gplt.choropleth(fifty_miles, hue = 'share_underrepresented', ax = ax)<jupyter_output><empty_output><jupyter_text>***EXTRA (this wasn't covered in the youtube tutorial)<jupyter_code>def make_cool_map(gdf, var_of_interest, ax, projection = gcrs.AlbersEqualArea(),):
    """makes a cool choropleth map from our data
    """
    proj = gcrs.AlbersEqualArea()

    ax = gplt.polyplot(
        county_shapes,     
        zorder=-1,
        projection = proj,
        linewidth=1,
        edgecolor='white',
        facecolor='lightgray',
        ax = ax,
    )
    
    gplt.choropleth(
        gdf, 
        hue = var_of_interest, 
        ax = ax, 
    )
proj = gcrs.WebMercator()
f, axarr = plt.subplots(2, 2, figsize=(12, 12), subplot_kw={'projection': proj})

plt.suptitle('Some cool maps', fontsize=16)
plt.subplots_adjust(top=0.95)

make_cool_map(fifty_miles, 'share_underrepresented', axarr[0][0])
axarr[0][0].set_title('Share URM')

make_cool_map(fifty_miles_inter, 'share_underrepresented', axarr[0][1])
axarr[0][1].set_title('Share URM')

make_cool_map(fifty_miles, 'median_hh_income', axarr[1][0])
axarr[1][0].set_title('Median household income')

make_cool_map(fifty_miles_inter, 'median_hh_income', axarr[1][1])
axarr[1][1].set_title('Median household income')

plt.savefig("test.png", bbox_inches='tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12987852158180407, 'fraction_numerical': 0.022615662962005685, 'mean_word_length': 7.003102378490175, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '218957', 'license': 'RSA-MD', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2822, 'n_words': 442, 'path': 'solutions/RQ3.ipynb', 'repo_name': 'chekos/geospatial-public-policy-analysis-with-geopandas', 'size': '7738'}