import pandas as pd

df = pd.read_csv('/opt/names/fec_contrib/contribDB_2000.csv', nrows=100)
df.columns

"""
Explanation: Application: Illustrating the use of the package by imputing the race of the campaign contributors recorded by FEC for the years 2000 and 2010
a) what proportion of contributors were black, whites, hispanics, asian etc.
b) and proportion of total donation given by blacks, hispanics, whites, and asians.  
c) get amount contributed by people of each race and divide it by total amount contributed.
End of explanation
"""


#df = pd.read_csv('/opt/names/fec_contrib/contribDB_2000.csv', usecols=['date', 'amount', 'contributor_type', 'contributor_lname', 'contributor_fname', 'contributor_name'], nrows=300000)
df = pd.read_csv('/opt/names/fec_contrib/contribDB_2000.csv', usecols=['date', 'amount', 'contributor_type', 'contributor_lname', 'contributor_fname', 'contributor_name'])
df

#sdf = df[df.contributor_type=='I'].sample(1000)
sdf = df[df.contributor_type=='I'].copy()
sdf

from clean_names import clean_name

def do_clean_name(n):
    n = str(n)
    return clean_name(n)

#sdf['clean_name'] = sdf['contributor_name'].apply(lambda c: do_clean_name(c))
#sdf

from ethnicolr import census_ln, pred_census_ln

rdf = pred_census_ln(sdf, 'contributor_lname', 2000)
rdf

#rdf.to_csv('output-pred-contrib2000-ln.csv', index_label='idx')

"""
Explanation: amount, date, contributor_name, contributor_lname, contributor_fname, contributor_type == 'I'
End of explanation
"""


adf = rdf.groupby(['race']).agg({'contributor_lname': 'count'})
adf *100 / adf.sum()

"""
Explanation: a) what proportion of contributors were black, whites, hispanics, asian etc.
End of explanation
"""


bdf = rdf.groupby(['race']).agg({'amount': 'sum'})
bdf * 100 / bdf.sum()

"""
Explanation: b) and proportion of total donation given by blacks, hispanics, whites, and asians.
End of explanation
"""


contrib_white = sum(rdf.amount * rdf.white)
contrib_black = sum(rdf.amount * rdf.black)
contrib_api = sum(rdf.amount * rdf.api)
contrib_hispanic = sum(rdf.amount * rdf.hispanic)

contrib_amount = [{'race': 'white', 'amount': contrib_white},
                              {'race': 'black', 'amount': contrib_black},
                              {'race': 'api', 'amount': contrib_api},
                              {'race': 'hispanic', 'amount': contrib_hispanic}]
contrib_df = pd.DataFrame(contrib_amount, columns=['race', 'amount'])
contrib_df.amount /= 10e6
contrib_df.columns = ['race', 'amount($1M)']
contrib_df

contrib_df.set_index('race', inplace=True, drop=True)
contrib_df.columns = ['% amount']
contrib_df * 100 / contrib_df.sum()

"""
Explanation: c) get amount contributed by people of each race and divide it by total amount contributed.
End of explanation
"""


