<jupyter_start><jupyter_text>Examining Political Donations<jupyter_code>from arcgis.gis import GIS
from IPython.display import display
import pandas as pd
gis = GIS(profile='agol_profile')<jupyter_output><empty_output><jupyter_text>Access and Geocode Address Data<jupyter_code>voter_file = gis.content.get("57894e2fb17b47b08c2608887a8e4897")
csv_file = voter_file.download(save_path=r"c:\temp", file_name="address.csv" )
df = pd.read_csv(csv_file, encoding="ISO-8859-1")
df.head()
geocoded_fc = gis.content.import_data(df=df)
geocoded_fc
gc_map = gis.map("Maryland")
gc_map.extent = {'spatialReference': {'latestWkid': 3857, 'wkid': 102100},
                 'xmin': -8586268.28378512,
                 'ymin': 4733599.9383514235,
                 'xmax': -8511665.744178887,
                 'ymax': 4764174.749665453}
gc_map
gc_map.add_layer(geocoded_fc)<jupyter_output><empty_output><jupyter_text>Access Census Data<jupyter_code>item = gis.content.get("2cb6e7dfc87047ccaa38f59d955d907b")
item
sdf = item.layers[0].query("CNTY2010 = '24027'", as_df=True)
sdf.head()
sdf.spatial.plot()<jupyter_output><empty_output><jupyter_text>Summarize Locations within Polygons<jupyter_code>from arcgis.features.summarize_data import summarize_within
result = summarize_within(sum_within_layer=sdf.spatial.to_feature_collection(), output_name="SummaryWithinOutput5",
                          summary_layer=geocoded_fc, sum_shape=True, summary_fields=["Donate SUM"])
result
m4 = gis.map()
m4.extent = result.extent
m4.basemap = 'dark-gray'
m4
m4.add_layer(result)<jupyter_output><empty_output><jupyter_text>Enrich Data to Locate More Information - use `enrich` to load the Age data<jupyter_code>from arcgis.geoenrichment import *
usa = Country.get('United States')
df = usa.data_collections
df.loc['Age']['analysisVariable'].unique()
count_sdf = result.layers[0].query(as_df=True)
count_sdf.head()
enrich_sdf = enrich(study_areas=count_sdf, data_collections=['Age'])
enrich_sdf.head()<jupyter_output><empty_output><jupyter_text>Find the Dollar Raised Per Person  Summarize the Total Population<jupyter_code>cols = [col for col in enrich_sdf.columns if (col.find("FEM") > -1 or col.find("MALE") > -1)]
enrich_sdf['TOTAL_POP'] = enrich_sdf[cols].sum(axis=1)<jupyter_output><empty_output><jupyter_text>Clean up the Data<jupyter_code>enrich_sdf["SUM_Donate"] = (enrich_sdf["SUM_Donate"]
                            .replace('NaN', 0)
                            .astype(int))<jupyter_output><empty_output><jupyter_text>Calculate Donation Dollars Per Person<jupyter_code>enrich_sdf["DOLLARPERPERSON"] = enrich_sdf['SUM_Donate'] / enrich_sdf['TOTAL_POP']
q = enrich_sdf["DOLLARPERPERSON"] > 0
target_tracts = enrich_sdf[q].copy().reset_index(drop=True)
m3 = gis.map("Maryland")
m3.basemap = 'dark-gray'
m3
m3.extent = target_tracts.spatial.full_extent
target_tracts.spatial.plot(map_widget=m3,
                           renderer_type='c',  # for class breaks renderer
                           method='esriClassifyNaturalBreaks',  # classification algorithm
                           class_count=10,  # choose the number of classes
                           col='DOLLARPERPERSON',  # numeric column to classify
                           cmap='prism_r',  # color map to pick colors from for each class
                           alpha=0.4)<jupyter_output><empty_output><jupyter_text>Generate Age Profile from Donations - Use matplotlib to display data in a visual way  Define the Pyramid<jupyter_code>def age_pyramid(df):
    import warnings
    import seaborn as sns
    import matplotlib.pyplot as plt

    %matplotlib inline
    warnings.simplefilter(action='ignore', category=FutureWarning)
    pd.options.mode.chained_assignment = None 
    plt.style.use('ggplot')

    df = df[[x for x in df.columns if 'MALE' in x or 'FEM' in x]]
    sf = pd.DataFrame(df.sum())
    age = sf.index.str.extract('(\d+)').astype('int64')
    f = sf[sf.index.str.startswith('FEM')]
    m = sf[sf.index.str.startswith('MALE')]
    sf = sf.reset_index(drop = True)
    f = f.reset_index(drop = True)
    m = m.reset_index(drop = True)
    sf['age'] = age
    f["age"] = age
    m["age"] = age
    f = f.sort_values(by='age', ascending=False).set_index('age')
    m = m.sort_values(by='age', ascending=False).set_index('age')
    

    popdf = pd.concat([f, m], axis=1)
    popdf.columns = ['F', 'M']
    popdf['agelabel'] = popdf.index.map(str) + ' - ' + (popdf.index+4).map(str)
    popdf.M = -popdf.M
    
    sns.barplot(x="F", y="agelabel", color="#CC6699", label="Female", data=popdf, edgecolor='none')
    sns.barplot(x="M",  y="agelabel", color="#008AB8", label="Male",   data=popdf,  edgecolor='none')
    plt.ylabel('Age group')
    plt.xlabel('Number of people');
    return plt;<jupyter_output><empty_output><jupyter_text>Generate the Age Pyramid for All Donations<jupyter_code>age_pyramid(target_tracts)<jupyter_output><empty_output><jupyter_text>Generate the Age Pyramid for Top Grossing Areas - The age population skews younger than the donation as a whole<jupyter_code>age_pyramid(target_tracts[target_tracts.DOLLARPERPERSON > 4].copy())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13218167701863354, 'fraction_numerical': 0.03202639751552795, 'mean_word_length': 4.395811518324607, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '343995', 'license': 'Apache-2.0', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1881, 'n_words': 319, 'path': 'talks/GeoDevWebinar_July02_2019/03 - analysis demo/03 - AnalysisWithArcGIS.ipynb', 'repo_name': 'test-v1/arcgis-python-api', 'size': '5152'}