import pandas as pd import jinja2 from collections import OrderedDict from json import dumps from IPython.html.widgets import interact from IPython.html import widgets from IPython.display import display, display_pretty, Javascript, HTML from IPython.utils.traitlets import Any, Bool, Dict, List, Unicode from threading import Lock from urllib2 import urlopen sub_est_2012_df = pd.read_csv( urlopen('http://www.census.gov/popest/data/cities/totals/2012/files/SUB-EST2012.csv'), encoding='latin-1', dtype={'STATE': 'str', 'COUNTY': 'str', 'PLACE': 'str'} ) sub_est_2012_df_by_county = sub_est_2012_df[sub_est_2012_df.SUMLEV == 50] sub_est_2012_df_by_state = sub_est_2012_df_by_county.groupby(['STATE']).sum() # Alternatively we could have just taken the summary rows for the states # sub_est_2012_df_by_state = sub_est_2012_df[sub_est_2012_df.SUMLEV == 40] # Taken from http://www.census.gov/geo/reference/ansi_statetables.html state = pd.read_csv(urlopen('http://www.census.gov/geo/reference/docs/state.txt'), sep='|', dtype={'STATE': 'str'}) state.drop( ['STATENS'], inplace=True, axis=1 ) sub_est_2012_df_by_state = pd.merge(sub_est_2012_df_by_state, state, left_index=True, right_on='STATE') sub_est_2012_df_by_state.drop( ['SUMLEV', 'COUSUB', 'CONCIT', 'ESTIMATESBASE2010', 'POPESTIMATE2010', 'POPESTIMATE2011'], inplace=True, axis=1 ) # Taken from http://www.census.gov/popest/data/state/asrh/2012/SC-EST2012-AGESEX-CIV.html sc_est2012_agesex_civ_df = pd.read_csv( urlopen('http://www.census.gov/popest/data/state/asrh/2012/files/SC-EST2012-AGESEX-CIV.csv'), encoding='latin-1', dtype={'SUMLEV': 'str'} ) sc_est2012_agesex_civ_df_sumlev040 = sc_est2012_agesex_civ_df[ (sc_est2012_agesex_civ_df.SUMLEV == '040') & (sc_est2012_agesex_civ_df.SEX != 0) & (sc_est2012_agesex_civ_df.AGE != 999) ] sc_est2012_agesex_civ_df_sumlev040.drop( ['SUMLEV', 'NAME', 'ESTBASE2010_CIV', 'POPEST2010_CIV', 'POPEST2011_CIV'], inplace=True, axis=1 ) sc_est2012_agesex_civ_df_sumlev040['STATE'] = sc_est2012_agesex_civ_df_sumlev040['STATE'].apply(lambda x: '%02d' % (x,)) sc_est2012_sex = sc_est2012_agesex_civ_df_sumlev040.groupby(['STATE', 'REGION', 'DIVISION', 'SEX'], as_index=False)[['POPEST2012_CIV']].sum() sc_est2012_sex = pd.merge(sc_est2012_sex, state, left_on='STATE', right_on='STATE') sc_est2012_age = sc_est2012_agesex_civ_df_sumlev040.groupby(['STATE', 'REGION', 'DIVISION', 'AGE'], as_index=False)[['POPEST2012_CIV']].sum() age_buckets = pd.cut(sc_est2012_age.AGE, range(0,100,20)) sc_est2012_age = sc_est2012_age.groupby(['STATE', 'REGION', 'DIVISION', age_buckets], as_index=False)['POPEST2012_CIV'].sum() sc_est2012_age = pd.merge(sc_est2012_age, state, left_on='STATE', right_on='STATE') region_codes = { 0: 'United States Total', 1: 'Northeast', 2: 'Midwest', 3: 'South', 4: 'West' } division_codes = { 0: 'United States Total', 1: 'New England', 2: 'Middle Atlantic', 3: 'East North Central', 4: 'West North Central', 5: 'South Atlantic', 6: 'East South Central', 7: 'West South Central', 8: 'Mountain', 9: 'Pacific' } %%javascript require.config({ paths: { d3: '//cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.min' } }); display(HTML("""
""")) sub_est_2012_df_by_state_template = jinja2.Template( """ // Based on http://bl.ocks.org/mbostock/3885304 require(["d3"], function(d3) { var data = [] {% for row in data %} data.push({ 'state': '{{ row[4] }}', 'population': {{ row[1] }} }); {% endfor %} d3.select("#chart_d3 svg").remove() var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 800 - margin.left - margin.right, height = 400 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .25); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(10) .tickFormat(d3.format('.1s')); var svg = d3.select("#chart_d3").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); x.domain(data.map(function(d) { return d.state; })); y.domain([0, d3.max(data, function(d) { return d.population; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Population"); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.state); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.population); }) .attr("height", function(d) { return height - y(d.population); }); }); """ ) display(Javascript(sub_est_2012_df_by_state_template.render( data=sub_est_2012_df_by_state.sort(['POPESTIMATE2012'], ascending=False)[:5].itertuples())) ) class MultipleSelectWidget(widgets.DOMWidget): _view_name = Unicode('MultipleSelectView', sync=True) value = List(sync=True) values = Dict(sync=True) values_order = List(sync=True) description = Unicode(sync=True) def __init__(self, *args, **kwargs): self.value_lock = Lock() self.values = kwargs.get('values', []) self.value = kwargs.get('value', []) self.values_order = kwargs.get('values_order', []) widgets.DOMWidget.__init__(self, *args, **kwargs) %%javascript require(["widgets/js/widget"], function(WidgetManager){ var MultipleSelectView = IPython.DOMWidgetView.extend({ initialize: function(parameters) { this.model.on('change',this.update,this); this.options = parameters.options; this.child_views = []; // I had to override DOMWidgetView's initialize to set model.views otherwise // multiple views would get attached to the model this.model.views = [this]; }, render : function(){ this.$el .addClass('widget-hbox'); this.$label = $('
') .appendTo(this.$el) .addClass('widget-hlabel') .hide(); this.$listbox = $('