import pandas as pd

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

from ethnicolr import pred_fl_reg_name, pred_fl_reg_ln

"""
Explanation: Application: 2000/2010 Political Campaign Contributions by Race
Using ethnicolr, we look to answer three basic questions:
<ol>
<li>What proportion of contributions were made by blacks, whites, Hispanics, and Asians? 
<li>What proportion of unique contributors were blacks, whites, Hispanics, and Asians?
<li>What proportion of total donations were given by blacks, whites, Hispanics, and Asians?
</ol>
End of explanation
"""


df = pd.read_csv('/opt/names/fec_contrib/contribDB_2000.csv', usecols=['amount', 'contributor_type', 'contributor_lname', 'contributor_fname', 'contributor_name'])
sdf = df[df.contributor_type=='I'].copy()
sdf.fillna('', inplace=True)
rdf2000 = pred_fl_reg_name(sdf, 'contributor_lname', 'contributor_fname')
rdf2000['year'] = 2000

df = pd.read_csv('/opt/names/fec_contrib/contribDB_2010.csv.zip', usecols=['amount', 'contributor_type', 'contributor_lname', 'contributor_fname', 'contributor_name'])
sdf = df[df.contributor_type=='I'].copy()
sdf.fillna('', inplace=True)
rdf2010 = pred_fl_reg_name(sdf, 'contributor_lname', 'contributor_fname')
rdf2010['year'] = 2010

rdf = pd.concat([rdf2000, rdf2010])
rdf.head(20)

"""
Explanation: Load and Subset on Individual Contributors
End of explanation
"""


adf = rdf.groupby(['year', 'race']).agg({'contributor_lname': 'count'})
adf.unstack().apply(lambda r: r / r.sum(), axis=1).style.format("{:.2%}")

"""
Explanation: What proportion of contributons were by blacks, whites, Hispanics, and Asians?
End of explanation
"""


udf = rdf.drop_duplicates(subset=['contributor_name']).copy()
gdf = udf.groupby(['year', 'race']).agg({'contributor_name': 'count'})
gdf.unstack().apply(lambda r: r / r.sum(), axis=1).style.format("{:.2%}")

"""
Explanation: What proportion of the donors were blacks, whites, Hispanics, and Asians?
End of explanation
"""


bdf = rdf.groupby(['year', 'race']).agg({'amount': 'sum'})
bdf.unstack().apply(lambda r: r / r.sum(), axis=1).style.format("{:.2%}")

"""
Explanation: What proportion of the total donation was given by blacks, whites, Hispanics, and Asians?
End of explanation
"""


rdf['white_count'] = rdf.nh_white
rdf['black_count'] = rdf.nh_black
rdf['asian_count'] = rdf.asian
rdf['hispanic_count'] = rdf.hispanic
gdf = rdf.groupby(['year']).agg({'white_count': 'sum', 'black_count': 'sum', 'asian_count': 'sum', 'hispanic_count': 'sum'})
gdf.apply(lambda r: r / r.sum(), axis=1).style.format("{:.2%}")

"""
Explanation: What if we estimated by using probabilities for race rather than labels?
What proportion of contributons were by blacks, whites, Hispanics, and Asians?
End of explanation
"""


udf['white_count'] = udf.nh_white
udf['black_count'] = udf.nh_black
udf['asian_count'] = udf.asian
udf['hispanic_count'] = udf.hispanic
gdf = udf.groupby(['year']).agg({'white_count': 'sum', 'black_count': 'sum', 'asian_count': 'sum', 'hispanic_count': 'sum'})
gdf.apply(lambda r: r / r.sum(), axis=1).style.format("{:.2%}")

"""
Explanation: What proportion of the donors were blacks, whites, Hispanics, and Asians?
End of explanation
"""


rdf['white_amount'] = rdf.amount * rdf.nh_white
rdf['black_amount'] = rdf.amount * rdf.nh_black
rdf['api_amount'] = rdf.amount * rdf.asian
rdf['hispanic_amount'] = rdf.amount * rdf.hispanic
gdf = rdf.groupby(['year']).agg({'white_amount': 'sum', 'black_amount': 'sum', 'api_amount': 'sum', 'hispanic_amount': 'sum'}) / 10e6
gdf.style.format("{:0.2f}")

gdf.apply(lambda r: r / r.sum(), axis=1).style.format("{:.2%}")

"""
Explanation: What proportion of the total donation was given by blacks, whites, Hispanics, and Asians?
End of explanation
"""


