Import agate, a Python-based data analysis library


In [2]:
import agate

Create agate Table of csv file from Nebraska E911 fund data from 2001 to 2015


In [3]:
column_fix = agate.TypeTester(force={
    'date': agate.Date(),
    'neb_notes': agate.Text()
})

funds = agate.Table.from_csv('e911_fund.csv', column_types=column_fix)

Check out column names & types from funds table


In [4]:
print(funds)


|----------------+---------------|
|  column_names  | column_types  |
|----------------+---------------|
|  date          | Date          |
|  neb_balance   | Number        |
|  neb_surcharge | Number        |
|  neb_notes     | Text          |
|----------------+---------------|

Calculate the number of Nebraska cell phones by dividing the e911 fund by the surcharge amount


In [5]:
def calculateNebCellPhones(row):
    return row['neb_balance'] / row['neb_surcharge']

cell_phones = funds.compute([
        ('cell_phones', agate.Formula(agate.Number(), calculateNebCellPhones))
    ])

Let's see the new table


In [6]:
cell_phones.print_table()


|-------------+--------------+---------------+------------------+-----------------------|
|        date |  neb_balance | neb_surcharge | neb_notes        |          cell_phones  |
|-------------+--------------+---------------+------------------+-----------------------|
|  2015-01-01 | 8,120,555.80 |          0.45 |                  | 18,045,679.555555...  |
|  2014-01-01 | 7,948,205.99 |          0.45 |                  | 17,662,679.977777...  |
|  2013-01-01 | 8,020,447.42 |          0.45 |                  | 17,823,216.488888...  |
|  2012-01-01 | 8,137,993.90 |          0.50 |                  | 16,275,987.800000...  |
|  2011-01-01 | 8,012,693.70 |          0.50 |                  | 16,025,387.400000...  |
|  2010-01-01 | 7,847,272.81 |          0.50 |                  | 15,694,545.620000...  |
|  2009-01-01 | 7,701,153.41 |          0.50 |                  | 15,402,306.820000...  |
|  2008-01-01 | 7,380,870.49 |          0.50 |                  | 14,761,740.980000...  |
|  2007-01-01 | 6,849,095.65 |          0.50 |                  | 13,698,191.300000...  |
|  2006-01-01 | 6,045,044.82 |          0.50 |                  | 12,090,089.640000...  |
|  2005-01-01 | 5,759,442.54 |          0.50 |                  | 11,518,885.080000...  |
|  2004-01-01 | 5,344,195.08 |          0.50 |                  | 10,688,390.160000...  |
|  2003-01-01 | 4,931,242.88 |          0.50 |                  | 9,862,485.7600000...  |
|  2002-01-01 | 4,556,158.34 |          0.50 |                  | 9,112,316.6800000...  |
|  2001-01-01 | 2,045,383.75 |          0.50 | July to Dec 2001 | 4,090,767.5000000...  |
|-------------+--------------+---------------+------------------+-----------------------|

Now, I'll round those awkward cell phone estimates


In [7]:
from decimal import Decimal

def round_cell_phones(row):
    return row['cell_phones'].quantize(Decimal('1'))

rounded_cell_phones = cell_phones.compute([
        ('cell_phones_round', agate.Formula(agate.Number(), round_cell_phones))
    ])

Let's see the nebraska e911 fund table


In [8]:
rounded_cell_phones.print_table()


|-------------+--------------+---------------+------------------+----------------------+--------------------|
|        date |  neb_balance | neb_surcharge | neb_notes        |          cell_phones | cell_phones_round  |
|-------------+--------------+---------------+------------------+----------------------+--------------------|
|  2015-01-01 | 8,120,555.80 |          0.45 |                  | 18,045,679.555555... |        18,045,680  |
|  2014-01-01 | 7,948,205.99 |          0.45 |                  | 17,662,679.977777... |        17,662,680  |
|  2013-01-01 | 8,020,447.42 |          0.45 |                  | 17,823,216.488888... |        17,823,216  |
|  2012-01-01 | 8,137,993.90 |          0.50 |                  | 16,275,987.800000... |        16,275,988  |
|  2011-01-01 | 8,012,693.70 |          0.50 |                  | 16,025,387.400000... |        16,025,387  |
|  2010-01-01 | 7,847,272.81 |          0.50 |                  | 15,694,545.620000... |        15,694,546  |
|  2009-01-01 | 7,701,153.41 |          0.50 |                  | 15,402,306.820000... |        15,402,307  |
|  2008-01-01 | 7,380,870.49 |          0.50 |                  | 14,761,740.980000... |        14,761,741  |
|  2007-01-01 | 6,849,095.65 |          0.50 |                  | 13,698,191.300000... |        13,698,191  |
|  2006-01-01 | 6,045,044.82 |          0.50 |                  | 12,090,089.640000... |        12,090,090  |
|  2005-01-01 | 5,759,442.54 |          0.50 |                  | 11,518,885.080000... |        11,518,885  |
|  2004-01-01 | 5,344,195.08 |          0.50 |                  | 10,688,390.160000... |        10,688,390  |
|  2003-01-01 | 4,931,242.88 |          0.50 |                  | 9,862,485.7600000... |         9,862,486  |
|  2002-01-01 | 4,556,158.34 |          0.50 |                  | 9,112,316.6800000... |         9,112,317  |
|  2001-01-01 | 2,045,383.75 |          0.50 | July to Dec 2001 | 4,090,767.5000000... |         4,090,768  |
|-------------+--------------+---------------+------------------+----------------------+--------------------|

Add the Iowa e911 fund information


In [9]:
column_names = ['date', 'ia_balance', 'ia_surcharge']
column_types = [agate.Date(), agate.Number(), agate.Number()]
iowa_phones = agate.Table.from_csv('e911_fund_iowa.csv', column_names, column_types)

In [10]:
print(iowa_phones)


|---------------+---------------|
|  column_names | column_types  |
|---------------+---------------|
|  date         | Date          |
|  ia_balance   | Number        |
|  ia_surcharge | Number        |
|---------------+---------------|

Calculate IA cell phones from e911 balance and surcharge amounts


In [11]:
def calculateIACellPhones(row):
    return row['ia_balance'] / row['ia_surcharge']

In [12]:
ia_cell_phones = iowa_phones.compute([
        ('ia_cell_phones', agate.Formula(agate.Number(), calculateIACellPhones))
    ])

In [13]:
ia_cell_phones.print_table()


|-------------+---------------+--------------+-----------------------|
|        date |    ia_balance | ia_surcharge |       ia_cell_phones  |
|-------------+---------------+--------------+-----------------------|
|  2006-01-01 |  2,259,443.41 |         0.65 | 3,476,066.7846153...  |
|  2007-01-01 |  2,457,508.25 |         0.65 | 3,780,781.9230769...  |
|  2008-01-01 |  3,577,089.62 |         0.65 | 5,503,214.8000000...  |
|  2009-01-01 |  4,501,061.70 |         0.65 | 6,924,710.3076923...  |
|  2010-01-01 | 11,015,639.09 |         0.65 | 16,947,137.061538...  |
|  2011-01-01 | 10,813,367.38 |         0.65 | 16,635,949.815384...  |
|  2012-01-01 | 11,074,150.06 |         0.65 | 17,037,153.938461...  |
|  2013-01-01 | 11,414,732.85 |         1.00 | 11,414,732.850000...  |
|  2014-01-01 | 21,045,807.15 |         1.00 | 21,045,807.150000...  |
|  2015-01-01 | 19,105,078.21 |         1.00 | 19,105,078.210000...  |
|-------------+---------------+--------------+-----------------------|

Round those cell phone amounts


In [14]:
from decimal import Decimal

def round_cell_phones_ia(row):
    return row['ia_cell_phones'].quantize(Decimal('1'))

rounded_cell_phones_ia = ia_cell_phones.compute([
        ('ia_cell_phones_round', agate.Formula(agate.Number(), round_cell_phones_ia))
    ])

In [15]:
rounded_cell_phones_ia.print_table()


|-------------+---------------+--------------+----------------------+-----------------------|
|        date |    ia_balance | ia_surcharge |       ia_cell_phones | ia_cell_phones_round  |
|-------------+---------------+--------------+----------------------+-----------------------|
|  2006-01-01 |  2,259,443.41 |         0.65 | 3,476,066.7846153... |            3,476,067  |
|  2007-01-01 |  2,457,508.25 |         0.65 | 3,780,781.9230769... |            3,780,782  |
|  2008-01-01 |  3,577,089.62 |         0.65 | 5,503,214.8000000... |            5,503,215  |
|  2009-01-01 |  4,501,061.70 |         0.65 | 6,924,710.3076923... |            6,924,710  |
|  2010-01-01 | 11,015,639.09 |         0.65 | 16,947,137.061538... |           16,947,137  |
|  2011-01-01 | 10,813,367.38 |         0.65 | 16,635,949.815384... |           16,635,950  |
|  2012-01-01 | 11,074,150.06 |         0.65 | 17,037,153.938461... |           17,037,154  |
|  2013-01-01 | 11,414,732.85 |         1.00 | 11,414,732.850000... |           11,414,733  |
|  2014-01-01 | 21,045,807.15 |         1.00 | 21,045,807.150000... |           21,045,807  |
|  2015-01-01 | 19,105,078.21 |         1.00 | 19,105,078.210000... |           19,105,078  |
|-------------+---------------+--------------+----------------------+-----------------------|

Join together nebraska and iowa tables


In [16]:
total_cell_phones = rounded_cell_phones.join(rounded_cell_phones_ia, 'date', 'date')

In [17]:
total_cell_phones.print_table()


|-------------+--------------+---------------+------------------+----------------------+-------------------+---------------+--------------+----------------------+-----------------------|
|        date |  neb_balance | neb_surcharge | neb_notes        |          cell_phones | cell_phones_round |    ia_balance | ia_surcharge |       ia_cell_phones | ia_cell_phones_round  |
|-------------+--------------+---------------+------------------+----------------------+-------------------+---------------+--------------+----------------------+-----------------------|
|  2015-01-01 | 8,120,555.80 |          0.45 |                  | 18,045,679.555555... |        18,045,680 | 19,105,078.21 |         1.00 | 19,105,078.210000... |           19,105,078  |
|  2014-01-01 | 7,948,205.99 |          0.45 |                  | 17,662,679.977777... |        17,662,680 | 21,045,807.15 |         1.00 | 21,045,807.150000... |           21,045,807  |
|  2013-01-01 | 8,020,447.42 |          0.45 |                  | 17,823,216.488888... |        17,823,216 | 11,414,732.85 |         1.00 | 11,414,732.850000... |           11,414,733  |
|  2012-01-01 | 8,137,993.90 |          0.50 |                  | 16,275,987.800000... |        16,275,988 | 11,074,150.06 |         0.65 | 17,037,153.938461... |           17,037,154  |
|  2011-01-01 | 8,012,693.70 |          0.50 |                  | 16,025,387.400000... |        16,025,387 | 10,813,367.38 |         0.65 | 16,635,949.815384... |           16,635,950  |
|  2010-01-01 | 7,847,272.81 |          0.50 |                  | 15,694,545.620000... |        15,694,546 | 11,015,639.09 |         0.65 | 16,947,137.061538... |           16,947,137  |
|  2009-01-01 | 7,701,153.41 |          0.50 |                  | 15,402,306.820000... |        15,402,307 |  4,501,061.70 |         0.65 | 6,924,710.3076923... |            6,924,710  |
|  2008-01-01 | 7,380,870.49 |          0.50 |                  | 14,761,740.980000... |        14,761,741 |  3,577,089.62 |         0.65 | 5,503,214.8000000... |            5,503,215  |
|  2007-01-01 | 6,849,095.65 |          0.50 |                  | 13,698,191.300000... |        13,698,191 |  2,457,508.25 |         0.65 | 3,780,781.9230769... |            3,780,782  |
|  2006-01-01 | 6,045,044.82 |          0.50 |                  | 12,090,089.640000... |        12,090,090 |  2,259,443.41 |         0.65 | 3,476,066.7846153... |            3,476,067  |
|  2005-01-01 | 5,759,442.54 |          0.50 |                  | 11,518,885.080000... |        11,518,885 |               |              |                      |                       |
|  2004-01-01 | 5,344,195.08 |          0.50 |                  | 10,688,390.160000... |        10,688,390 |               |              |                      |                       |
|  2003-01-01 | 4,931,242.88 |          0.50 |                  | 9,862,485.7600000... |         9,862,486 |               |              |                      |                       |
|  2002-01-01 | 4,556,158.34 |          0.50 |                  | 9,112,316.6800000... |         9,112,317 |               |              |                      |                       |
|  2001-01-01 | 2,045,383.75 |          0.50 | July to Dec 2001 | 4,090,767.5000000... |         4,090,768 |               |              |                      |                       |
|-------------+--------------+---------------+------------------+----------------------+-------------------+---------------+--------------+----------------------+-----------------------|

I wanted to calculate some stats by just grabing the amounts from the table


In [18]:
ia_phones_2015 = total_cell_phones.columns['ia_cell_phones_round'][0]
ia_phones_2006 = total_cell_phones.columns['ia_cell_phones_round'][9]
ia_pc_change = ( ia_phones_2015 - ia_phones_2006)/ia_phones_2006

In [19]:
print("Iowa percent change in cell phones for 2015, {}, to 2006 {} is {}".format(ia_phones_2015, ia_phones_2006, ia_pc_change))


Iowa percent change in cell phones for 2015, 19105078, to 2006 3476067 is 4.496176569669111671322790959

In [20]:
neb_phones_2015 = total_cell_phones.columns['cell_phones_round'][0]
neb_phones_2006 = total_cell_phones.columns['cell_phones_round'][9]
neb_pc_change = (neb_phones_2015 - neb_phones_2006)/neb_phones_2006

In [21]:
print("Neb percent change in cell phones for 2015, {}, to 2006 {} is {}".format(neb_phones_2015, neb_phones_2006, neb_pc_change))


Neb percent change in cell phones for 2015, 18045680, to 2006 12090090 is 0.4926009649225109159650589863

In [22]:
neb_phones_2001 = total_cell_phones.columns['cell_phones_round'][14]
neb_pc_change_total = (neb_phones_2015 - neb_phones_2001)/neb_phones_2001

In [23]:
print("Neb percent change in cell phones for 2015, {}, to 2001 {} is {}".format(total_cell_phones.columns['cell_phones_round'][0], total_cell_phones.columns['cell_phones_round'][14], neb_pc_change_total))


Neb percent change in cell phones for 2015, 18045680, to 2001 4090768 is 3.411318363691121080442596598

Calculate the percent cell phone numbers with previously estimated cell phones and US census pop estimates


In [24]:
neb_pop_15 = 1896190
neb_pop_06 = 1796619
ia_pop_15 = 3123899
ia_pop_06 = 2982644

In [25]:
neb_pc_cell15 = (neb_phones_2015/neb_pop_15)
neb_pc_cell06 = (neb_phones_2006/neb_pop_06)
ia_pc_cell15 = (ia_phones_2015/ia_pop_15)
ia_pc_cell06 = (ia_phones_2006/ia_pop_06)

In [26]:
print('Neb cell 15 {}, neb cell 06 {}, ia cell 15 {}, ia cell 06 {}'.format(neb_pc_cell15, neb_pc_cell06, ia_pc_cell15, ia_pc_cell06))


Neb cell 15 9.516810024311909671499164113, neb cell 06 6.729356641558393849781172302, ia cell 15 6.115779671493860717007816194, ia cell 06 1.165431409179238286567220225

In [100]:
total_cell_phones.to_csv('clean_cell_phone_fund.csv')

In [27]:
json_table = total_cell_phones.select(['date', 'cell_phones_round', 'ia_cell_phones_round'])
json_table.to_json('cellphones.js')

In [ ]: