# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import geopy
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

# # Donor-Advised Fund Tracker
# ##### Exploratory Data Analysis 
# ----
# This project aims to analyze and map the flow of funds from donor-advised funds to nonprofit organizations, as well as demonstrate a sustainable open-source information project for the nonprofit sector.

grantees = pd.read_csv('Grantees.csv')

sponsors = pd.read_csv('Sponsors.csv')

print('Number of nonunique grantee organizations: ', grantees.shape[0])
print('Number of sponsoring organizations: ', sponsors.shape[0])

# The *sponsors* dataframe indicates the sponsoring organizations of any donor-advised funds, and the *grantees* dataframe aggregates information about the recipients. See [fields.csv](https://github.com/leppekja/IRS-990-DAFs/blob/master/fields.csv) as a reference for field descriptions.

sponsors.head()

grantees.head()

# ### Data Cleaning
# Both dataframes:
#
# - remove Unnamed:0 index column
# - 8 digit zip codes to 5 
# - Check state abbreviations, address standardization
#
# grantees.csv
#
# - remove trailing zero on ZIPCd, RecipientEIN,

# +
grantees.drop(['Unnamed: 0'], axis=1, inplace=True)
sponsors.drop(['Unnamed: 0'], axis=1, inplace=True)

grantees['ZIPCd'] = grantees['ZIPCd'].astype(str).str[:5]
sponsors['ZIPCd'] = sponsors['ZIPCd'].astype(str).str[:5]

# Grantees may be school districts, meaning no EIN
grantees['RecipientEIN'].fillna(-1, inplace=True)
grantees['RecipientEIN'] = grantees['RecipientEIN'].astype(int)
# -

# ### Data Visualizations
# ----
# - how many nonprofit organizations supported by each sponsor bar chart
# - how much funding given by each sponsor bar chart
# - aggregate income into daf for each sponsor bar chart
# - how much funding recieved by each nonprofit organization bar chart
# - functions for mapping sponsors outgoing funds
# - functions for mapping grantee's incoming funds

fig, ax = plt.subplots(figsize=(12,3))
grantees.groupby('Sponsor')['RecipientEIN'].count().plot(kind='bar', ax=ax)

grantees['RecipientEIN'].nunique()

sponsors.DonorAdvisedFundsHeldCnt.sum()


def frequency(data, columns):
    '''
    Displays general visualizations to explore data
    '''
    fig, axs = plt.subplots(len(columns), figsize=(15,4), sharex=False)
    fig.subplots_adjust(hspace=1)
    plt.suptitle("Frequency and Proportions of " + ", ".join(columns))
    axes_iterator = ([axs] if len(columns) == 1 else axs.flatten())
    for ax, col in zip(axes_iterator, columns):
        twin = ax.twinx()
        ax.bar(data[col].value_counts().index, data[col].value_counts().values)
        twin.plot(np.cumsum(data[col].value_counts(normalize=True)), c='r')
        twin.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))
        plt.setp(ax.get_xticklabels(), rotation=30, ha='right', fontsize=12)
        ax.set_ylabel('Frequency')
        twin.set_ylabel('Proportion')
        ax.grid(b=True, which='major')
        ax.set_axisbelow(b=True)
    
    plt.show()


sponsors[sponsors['DonorAdvisedFundsHeldCnt'] == sponsors['DonorAdvisedFundsHeldCnt'].max()]

sponsors.iloc[sponsors['DonorAdvisedFundsHeldCnt'].nlargest(10).index]

# Quick geocoding test using the Census Bureau's [geocoding service](https://geocoding.geo.census.gov/geocoder/locations/addressbatch?form):

sponsor_addresses = sponsors.loc[:,['AddressLine1Txt','CityNm','StateAbbreviationCd','ZIPCd']].to_csv('sponsor_addresses.csv')
grantee_addresses_1 = grantees.loc[:9999,['AddressLine1Txt','CityNm','StateAbbreviationCd','ZIPCd']].to_csv('grantee_addresses_1.csv')
grantee_addresses_2 = grantees.loc[9999:,['AddressLine1Txt','CityNm','StateAbbreviationCd','ZIPCd']].to_csv('grantee_addresses_2.csv')


grantees.columns

grantees.shape

sponsors_locs = gpd.GeoDataFrame(pd.read_csv('sponsor_address_results.csv'))

sponsors_locs.head()

sponsors_locs['Unnamed: 0'].str.split(',')



StarcoderdataJupyterScriptsDedupFiltered