In [235]:
import plotly as py
import pandas as pd
from plotly.graph_objs import *
# py.offline.init_notebook_mode()
In [259]:
# py.tools.set_credentials_file(username='celine103', api_key='86dvK98s8grMghXK2z0d')
py.tools.set_credentials_file(username='alexbear', api_key='L6m9DmfDjqrksfHtUH5j')
# py.tools.set_credentials_file(username='celine3729', api_key='yii9AN8IEdtEkbwToSg8')
In [237]:
df2 = pd.read_csv('DiscGrants96to13.csv',encoding='iso-8859-1')
df2.head()
Out[237]:
In [234]:
py.__version__
Out[234]:
In [238]:
df3=df2[(df2["Longitude"]>-95)&(df2["Latitude"]<24)]
#df2=df2[(((df2["Longitude"]>-95)&(df2["Latitude"]<24))==False)]
df2=df2[((df2["InstState"]=="GU")|(df2["InstState"]=="PW")|(df2["InstState"]=="VI")|(df2["InstState"]=="PR")|(df2["InstState"]=="MP")|(df2["InstState"]=="FM")|(df2["InstState"]=="MH")|(df2["InstState"]=="AS")|(df2["InstState"]==""))==False]
In [247]:
yearly=[]
state=[]
for year,group in df2.groupby('FiscalYear'):
print (year)
df4=group.groupby('InstState')
# state=df4['InstState'].unique()
state.append(df4.groups)
total=df4['AwardTotal'].sum()
print(total)
yearly.append(total)
#print(yearly[0])
In [ ]:
In [306]:
scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],[0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
for year,group in df2.groupby('FiscalYear'):
print (year)
df4=group.groupby('InstState')
state=[]
amount=[]
for name,group in df4:
state.append(str(name))
amount.append(str(group['AwardTotal'].sum()))
# state=df4['InstState'].unique()
# state.append(df4.groups)
location = pd.DataFrame(state,columns = ['state'])
total= pd.DataFrame(amount,columns = ['amount'])
# yearly.append(total)
# for i in range(3):
data = [ dict(
type='choropleth',
colorscale = scl,
# autocolorscale = False,
# locations=df['InstState'].unique(),
# locations=state[i],
# z = yearly[i],
locations = location['state'],
z = total['amount'],
# df4['text'] = 'State: ' + df4.groups+ '<br>Total Amount: '+ z + ' dollar',
text = 'State: ' + location['state'] + '<br>Total Amount: '+ total['amount'] + ' dollar',
locationmode = 'USA-states',
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
) ),
colorbar = dict(
title = "Millions USD")
) ]
layout = dict(
title = str(1996+i)+'US Administrative Discretionary Grants<br>(Hover for breakdown)',
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showlakes = True,
lakecolor = 'rgb(255, 255, 255)'),
)
fig = dict( data=data, layout=layout )
py.plotly.iplot( fig)
break
#py.iplot(fig)
In [245]:
df.columns
In [ ]: