{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import pandas as pd\n", "from pandas import DataFrame\n", "\n", "import census\n", "import settings\n", "import us\n", "\n", "from itertools import islice\n", "\n", "# instantiate the census object\n", "\n", "c=census.Census(settings.CENSUS_KEY)\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "def places(variables=\"NAME\"):\n", " \n", " for state in us.states.STATES:\n", " print state\n", " geo = {'for':'place:*', 'in':'state:{s_fips}'.format(s_fips=state.fips)}\n", " for place in c.sf1.get(variables, geo=geo):\n", " yield place\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "r = list(islice(places(\"NAME,P0010001\"), None))\n", "places_df = DataFrame(r)\n", "places_df.P0010001 = places_df.P0010001.astype('int')\n", "\n", "places_df['FIPS'] = places_df.apply(lambda s: s['state']+s['place'], axis=1)\n", "\n", "print \"number of places\", len(places_df)\n", "print \"total pop\", places_df.P0010001.sum()\n", "places_df.head()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Alabama\n", "Alaska" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Arizona" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Arkansas" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "California" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Colorado" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Connecticut" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Delaware" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "District of Columbia" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Florida" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Georgia" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Hawaii" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Idaho" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Illinois" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Indiana" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Iowa" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Kansas" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Kentucky" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Louisiana" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Maine" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Maryland" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Massachusetts" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Michigan" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Minnesota" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Mississippi" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Missouri" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Montana" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Nebraska" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Nevada" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "New Hampshire" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "New Jersey" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "New Mexico" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "New York" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "North Carolina" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "North Dakota" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Ohio" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Oklahoma" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Oregon" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Pennsylvania" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Rhode Island" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "South Carolina" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "South Dakota" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Tennessee" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Texas" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Utah" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Vermont" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Virginia" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Washington" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "West Virginia" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Wisconsin" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Wyoming" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "number of places" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 29261\n", "total pop 228457238\n" ] }, { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NAMEP0010001placestateFIPS
0 Abanda CDP 192 00100 01 0100100
1 Abbeville city 2688 00124 01 0100124
2 Adamsville city 4522 00460 01 0100460
3 Addison town 758 00484 01 0100484
4 Akron town 356 00676 01 0100676
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ " NAME P0010001 place state FIPS\n", "0 Abanda CDP 192 00100 01 0100100\n", "1 Abbeville city 2688 00124 01 0100124\n", "2 Adamsville city 4522 00460 01 0100460\n", "3 Addison town 758 00484 01 0100484\n", "4 Akron town 356 00676 01 0100676" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "assert places_df.P0010001.sum() == 228457238\n", "# number of places in 2010 Census\n", "assert len(places_df) == 29261" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }