In [1]:
import pandas as pd
import numpy as np
%matplotlib inline

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

init_notebook_mode(connected=True)



In [2]:
df = pd.read_stata('~/../../statadata/IPEDSDirInfo02to18.dta')

In [8]:
state_counts_2018 = pd.DataFrame(df[df['isYr'] == 2018]['stabbr'].value_counts())
state_counts_2018.rename(columns = {'stabbr':'Total Institutions'}, inplace=True)
scratch = {}

for sec in df['sector'].unique():
    scratch[sec] = pd.DataFrame(df[(df['isYr'] == 2018) & 
                                   (df['sector'] == sec)]['stabbr'].value_counts())
    
    scratch[sec].rename(columns = {'stabbr':sec}, inplace=True)
    state_counts_2018 = pd.merge(state_counts_2018, scratch[sec], 
                                 left_index=True, 
                                 right_index=True,
                                 how='outer')

state_counts_2018.reset_index(inplace=True)

In [12]:
state_counts_2018.head()


Out[12]:
index Total Institutions Public, 2-year Sector unknown (not active) Public, 4-year or above Private not-for-profit, 4-year or above Administrative Unit Private for-profit, less-than 2-year Private for-profit, 4-year or above Private not-for-profit, 2-year Private for-profit, 2-year Public, less-than 2-year Private not-for-profit, less-than 2-year
0 AK 10 NaN NaN 4.0 2.0 1.0 NaN NaN 1.0 1.0 1.0 NaN
1 AL 90 26.0 NaN 14.0 21.0 1.0 16.0 4.0 NaN 7.0 NaN 1.0
2 AR 87 22.0 NaN 11.0 14.0 2.0 29.0 2.0 4.0 1.0 2.0 NaN
3 AS 1 NaN NaN 1.0 NaN NaN NaN NaN NaN NaN NaN NaN
4 AZ 122 20.0 2.0 10.0 12.0 2.0 37.0 19.0 NaN 17.0 2.0 1.0

In [18]:
data = {'type':'choropleth', 
        'locations':state_counts_2018['index'],
        'locationmode':'USA-states',
        'colorscale':'Greens',
        'reversescale':True,
        'z':state_counts_2018['Total Institutions'],
        'colorbar':{'title':'Counts'}}

layout = {'geo':{'scope':'usa'}}

choromap = go.Figure(data=[data], layout=layout)

In [20]:
iplot(choromap)



In [ ]: