Skip to content

Instantly share code, notes, and snippets.

@evz
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evz/ecba77631c4189a4e796 to your computer and use it in GitHub Desktop.
Save evz/ecba77631c4189a4e796 to your computer and use it in GitHub Desktop.
Reimagining Jonathans script
# This python file pulls the data for Chicago at a census tract level
# from plenario's api for the listed 311 call datasets below
import requests
import time
import sys
import csv
import os
datasets=['311_service_requests_abandoned_vehicles',
'311_service_requests_alley_lights_out', '311_service_requests_tree_trims',
'311_service_requests_street_lights_all_out', '311_service_requests_pot_holes_reported',
'311_service_requests_garbage_carts', '311_service_requests_sanitation_code_complaints',
'311_service_requests_graffiti_removal', '311_service_requests_vacant_and_abandoned_building',
'311_service_requests_tree_debris', '311_service_requests_rodent_baiting',
'311_service_requests_street_lights_one_out']
# plenario can technically handle querying all datasets at once using /v1/api/timeseries,
# but in practice it is too slow
tract_info = []
with open('tracts.csv', 'r') as f:
tract_info = list(csv.DictReader(f))
url = 'http://plenar.io/v1/api/timeseries/'
totals = []
for census_tract in tract_info:
outp_name = '%s.csv' % census_tract['Tract']
print('working on %s' % census_tract['Tract'])
if not os.path.exists(outp_name):
params = {
'agg': 'month',
'obs_date__ge': '2011-01-01',
'dataset_name__in': ','.join(datasets),
'census_block__like': '17031%s%%' % census_tract['Tract'],
'data_type': 'csv'
}
resp = requests.get(url, params=params)
with open(outp_name, 'w') as outp:
outp.write(resp.content.decode('utf-8'))
with open(outp_name, 'r') as f:
rows = list(csv.DictReader(f))
tract_totals = {d: 0 for d in datasets}
for dataset in datasets:
try:
tract_totals[dataset] = sum([int(r[dataset]) for r in rows])
except KeyError:
tract_totals[dataset] = 0
outp_row = {
'tract': census_tract['Tract'],
'population': census_tract['Population'],
'community_area': census_tract['CA']
}
outp_row.update(tract_totals)
totals.append(outp_row)
with open('totals.csv', 'w') as f:
writer = csv.DictWriter(f, fieldnames=totals[0].keys())
writer.writeheader()
writer.writerows(totals)
Tract CA Population
010100 01 5163
010201 01 6752
010202 01 2557
010300 01 6049
010400 01 4690
010501 01 3995
010502 01 4098
010503 01 2098
010600 01 7170
010701 01 4066
010702 01 5789
830600 01 5132
020100 02 3684
020200 02 6699
020301 02 4990
020302 02 4445
020400 02 4253
020500 02 6086
020601 02 6939
020602 02 4057
020701 02 1886
020702 02 7546
020801 02 5434
020802 02 7492
020901 02 5712
020902 02 4482
031000 03 3363
031100 03 4131
031200 03 5343
031300 03 7142
031400 03 5381
031501 03 3658
031502 03 5052
031700 03 6704
031800 03 1825
031900 03 2343
032100 03 6849
830700 03 3204
040100 04 4041
040201 04 5895
040202 04 6727
040300 04 2465
040401 04 3176
040402 04 4538
040600 04 2552
040700 04 3149
040800 04 1687
040900 04 2044
830800 04 2348
050100 05 2366
050200 05 4761
050300 05 2576
050500 05 5160
050600 05 2274
050700 05 1509
050800 05 1347
050900 05 1531
051000 05 1492
051100 05 1555
051200 05 1670
051300 05 2749
051400 05 2247
843700 05 2181
060100 06 2566
060200 06 1927
060300 06 3103
060400 06 4157
060500 06 1160
060800 06 4841
060900 06 6395
061000 06 1757
061100 06 1135
061200 06 2214
061500 06 1896
061800 06 1066
061901 06 3773
061902 06 3992
062000 06 2650
062100 06 3215
062200 06 2941
062300 06 1638
062400 06 1819
062500 06 1335
062600 06 2330
062700 06 2665
062800 06 3561
062900 06 4284
063000 06 3393
063100 06 2390
063200 06 6344
063301 06 2484
063302 06 3830
063303 06 1224
063400 06 1943
831900 06 2987
832000 06 2185
832100 06 3339
070101 07 3963
070102 07 2328
070103 07 1850
070200 07 4124
070300 07 3303
070400 07 3275
070500 07 3037
070600 07 2905
070700 07 3116
071000 07 4032
071100 07 2880
071200 07 2055
071300 07 3604
071400 07 4571
071500 07 5487
071600 07 1849
071700 07 1785
071800 07 2643
832500 07 3534
832600 07 4290
080100 08 6112
080201 08 3420
080202 08 3386
080300 08 4322
080400 08 4579
081000 08 7744
081100 08 3465
081201 08 4102
081202 08 3079
081300 08 4978
081401 08 2512
081402 08 4112
081403 08 6528
081500 08 4074
081600 08 3192
081700 08 3006
081800 08 6721
081900 08 1193
838300 08 1337
842200 08 2654
090100 09 3178
090200 09 6642
090300 09 1315
805600 09 0
100100 10 5960
100200 10 7188
100300 10 6024
100400 10 3025
100500 10 5659
100600 10 4694
100700 10 4631
810400 10 248
110100 11 5702
110200 11 3034
110300 11 4819
110400 11 4844
110501 11 4901
110502 11 3471
120100 12 4073
120200 12 4251
120300 12 6320
120400 12 3898
807900 12 0
808100 12 0
130100 13 6002
130200 13 1586
130300 13 4977
831800 13 6174
140100 14 3599
140200 14 6468
140301 14 2648
140302 14 4351
140400 14 6855
140500 14 4449
140601 14 3171
140602 14 5774
140701 14 2520
140702 14 6330
140800 14 7732
150200 15 7799
150300 15 6814
150401 15 4483
150402 15 3749
150501 15 3618
150502 15 4822
150600 15 3650
150700 15 4451
150800 15 5353
151001 15 2628
151002 15 5087
151100 15 4939
151200 15 3651
831700 15 2575
160100 16 2655
160200 16 3916
160300 16 3853
160400 16 5179
160501 16 4391
160502 16 4851
160601 16 3792
160602 16 3836
160700 16 5879
160800 16 5385
160900 16 2612
161000 16 1745
161100 16 2255
161200 16 2512
161300 16 3652
170100 17 2145
170200 17 3840
170300 17 5074
170400 17 3791
170500 17 4679
170600 17 2649
170700 17 3174
170800 17 3645
170900 17 1715
171000 17 7032
171100 17 4371
180100 18 5662
831600 18 6739
190100 19 2690
190200 19 6026
190300 19 5268
190401 19 4545
190402 19 4901
190601 19 4474
190602 19 4967
190701 19 2935
190702 19 4492
190800 19 6774
190900 19 2184
191000 19 2720
191100 19 7714
191200 19 4011
191301 19 5997
191302 19 4466
831500 19 3721
200100 20 4549
200200 20 5546
200300 20 2258
200401 20 4014
200402 20 3942
831200 20 4759
210100 21 4298
210400 21 3163
210501 21 4505
210502 21 5172
210601 21 3318
210602 21 4468
210700 21 4518
210800 21 1235
210900 21 3366
831100 21 7499
220300 22 2425
220400 22 2445
220500 22 2820
220601 22 1506
220602 22 4547
220701 22 3810
220702 22 4295
220901 22 3392
220902 22 3214
221000 22 3081
221100 22 4785
221200 22 2335
221300 22 2646
221400 22 3104
221500 22 2693
221600 22 2983
222200 22 1905
222500 22 1433
222600 22 1629
222700 22 2438
222800 22 881
222900 22 1274
830900 22 2785
831000 22 2361
832200 22 3102
832300 22 2247
832400 22 3650
230100 23 1519
230200 23 1624
230300 23 1120
230400 23 2650
230500 23 3490
230600 23 8092
230700 23 5178
230800 23 978
230900 23 5188
231100 23 1135
231200 23 5974
231500 23 5396
836600 23 2825
836700 23 2652
842100 23 6530
240200 24 1601
240300 24 1315
240500 24 2133
240600 24 1167
240700 24 1847
240800 24 1684
240900 24 963
241000 24 2283
241100 24 3931
241200 24 2043
241300 24 1902
241400 24 4834
241500 24 2904
241600 24 3545
242000 24 4409
242100 24 3235
242200 24 3989
242300 24 3540
242400 24 2956
242500 24 3993
242600 24 5669
242700 24 1839
242800 24 1626
242900 24 2022
243000 24 2369
243100 24 2509
243200 24 2070
243300 24 2183
243400 24 3052
243500 24 3169
842300 24 3305
250200 25 2642
250300 25 4916
250400 25 6402
250500 25 7489
250600 25 4962
250700 25 5979
250800 25 2272
251000 25 1284
251100 25 4811
251200 25 3946
251300 25 4774
251400 25 3952
251500 25 4227
251600 25 3174
251700 25 1613
251800 25 4954
251900 25 6063
252000 25 5973
252101 25 1559
252102 25 6110
252201 25 2653
252202 25 4391
831300 25 1284
831400 25 2732
260100 26 1283
260200 26 1140
260300 26 1484
260400 26 1520
260500 26 1716
260600 26 2945
260700 26 2408
260800 26 2245
260900 26 1938
261000 26 2698
270500 27 1660
271200 27 1342
271300 27 1150
271400 27 1299
271500 27 1395
271800 27 1017
836800 27 3284
836900 27 1614
837000 27 2438
837100 27 1991
837300 27 2119
837400 27 2007
280100 28 5293
280400 28 950
280800 28 544
280900 28 1039
281900 28 4049
282700 28 2505
282800 28 1726
283100 28 2519
283200 28 1255
283800 28 3196
832900 28 2611
833000 28 2603
833100 28 7086
833300 28 2336
837800 28 2964
838000 28 2758
838100 28 1851
838200 28 1695
841900 28 4830
842900 28 2341
290900 29 4038
291200 29 2440
291600 29 920
292200 29 3043
292400 29 2028
292500 29 4136
838600 29 1880
838700 29 4135
841400 29 1514
841500 29 2459
841600 29 916
843000 29 2862
843100 29 2485
843300 29 1458
843400 29 1760
300500 30 2888
300600 30 3458
300700 30 4451
300800 30 3970
300900 30 3718
301100 30 2583
301200 30 3974
301600 30 4458
301701 30 3578
301702 30 3387
301801 30 4315
301802 30 2509
301803 30 4976
830500 30 4424
840700 30 3954
840800 30 3582
841700 30 1349
843500 30 8983
310200 31 1412
310300 31 1547
310400 31 1363
310500 31 1659
310600 31 5644
310700 31 1538
310800 31 4875
310900 31 5332
841200 31 5792
841300 31 4354
843200 31 3061
320100 32 9603
320400 32 1546
320600 32 3670
839000 32 7405
839100 32 4139
330100 33 15740
330200 33 3872
841000 33 633
340300 34 1957
340400 34 1954
340500 34 1593
340600 34 1195
841100 34 7011
350100 35 1980
350400 35 857
351000 35 3711
351100 35 1919
351400 35 1366
351500 35 176
839200 35 2241
839500 35 1658
839600 35 1817
842000 35 3386
360200 36 1330
836400 36 3318
836500 36 1612
835500 37 1851
835600 37 1073
380100 38 2101
380200 38 1552
380500 38 697
380700 38 1590
381200 38 2278
381400 38 1842
381500 38 211
381700 38 0
381800 38 1198
381900 38 1414
835700 38 0
835800 38 1386
835900 38 3553
836000 38 2738
843600 38 2591
390100 39 1324
390200 39 1884
390300 39 2954
390400 39 2946
390500 39 1507
390600 39 1986
390700 39 5579
400300 40 1671
400400 40 1460
400500 40 2147
400800 40 2613
834500 40 1634
836100 40 1621
410100 41 1810
410200 41 1004
410500 41 2673
410600 41 2490
410700 41 1997
410800 41 3074
410900 41 3370
411000 41 3565
411100 41 2234
411200 41 1677
836200 41 1378
836300 41 1269
420100 42 1284
420200 42 1729
420300 42 1901
420400 42 1421
420500 42 2320
420600 42 1656
420700 42 2879
420800 42 2502
421200 42 1484
834400 42 3629
843900 42 3640
430101 43 3997
430102 43 3109
430200 43 4442
430300 43 2001
430400 43 3139
430500 43 3490
430600 43 1693
430700 43 2399
430800 43 1834
430900 43 1859
431200 43 2855
431301 43 2898
431302 43 2981
431400 43 6292
834200 43 4537
440101 44 4489
440102 44 3252
440201 44 5323
440202 44 3361
440300 44 4400
440600 44 2022
440700 44 1731
440800 44 2139
440900 44 2858
842400 44 3669
450300 45 3677
834300 45 5940
460100 46 3372
460200 46 2272
460301 46 3205
460302 46 3827
460400 46 3186
460500 46 5241
460600 46 1224
460700 46 2989
461000 46 1950
833900 46 2192
470100 47 3508
480100 48 1963
480200 48 1353
480300 48 1405
480400 48 6572
480500 48 3089
490300 49 2056
490400 49 1113
490500 49 2420
490600 49 1353
490700 49 3261
490800 49 3817
490901 49 4090
490902 49 4763
491000 49 4951
491100 49 4797
491200 49 2977
491300 49 2724
491400 49 3205
834000 49 3758
500100 50 3585
500200 50 2003
500300 50 1674
510100 51 3854
510200 51 3055
510300 51 6186
838800 51 3350
520100 52 1887
520200 52 4073
520300 52 6377
520400 52 3986
520500 52 4414
520600 52 2780
530100 53 2726
530200 53 5334
530300 53 4223
530400 53 2169
530501 53 5839
530502 53 1463
530503 53 5632
530600 53 3373
821402 53 51
540101 54 3509
540102 54 3311
550100 55 7497
550200 55 2671
560100 56 1102
560200 56 1997
560300 56 3166
560400 56 1422
560700 56 3242
560800 56 5600
560900 56 5186
561000 56 4865
561100 56 5735
835200 56 1625
980100 56 0
570100 57 1172
570200 57 1740
570300 57 6322
570400 57 1673
570500 57 2527
580100 58 3006
580200 58 3476
580300 58 2141
580400 58 3757
580501 58 5346
580502 58 5042
580600 58 6056
580700 58 5803
580800 58 1833
842800 58 7688
590500 59 1961
590600 59 4132
590700 59 3141
840300 59 4034
840400 59 3166
600400 60 4345
600600 60 2343
600700 60 2261
600900 60 3443
839700 60 5294
839800 60 3028
839900 60 3862
840000 60 2713
840100 60 2624
840200 60 2517
610300 61 5319
610400 61 1461
610800 61 1415
611000 61 1144
611100 61 2570
611200 61 2343
611300 61 3071
611400 61 3832
611500 61 3133
611600 61 2295
611700 61 1287
611800 61 2556
611900 61 2399
612000 61 1581
612100 61 1335
842600 61 4408
843800 61 1958
620100 62 4701
620200 62 2453
620300 62 6525
620400 62 5301
630100 63 1198
630200 63 2344
630300 63 5850
630400 63 6957
630500 63 5964
630800 63 6889
630900 63 4895
835100 63 5395
640100 64 1026
640300 64 6198
640400 64 3317
640500 64 3323
640600 64 4049
640700 64 3173
640800 64 1630
650100 65 4992
650200 65 7055
650301 65 5554
650302 65 4093
650400 65 6855
650500 65 4401
660301 66 1735
660302 66 5633
660400 66 5415
660500 66 5300
660600 66 6428
660700 66 2124
660800 66 6058
660900 66 4701
661000 66 5680
661100 66 5904
835000 66 5829
670100 67 1420
670200 67 1138
670300 67 1422
670400 67 2111
670500 67 1268
670600 67 1754
670700 67 1325
670800 67 1754
670900 67 1350
671100 67 1617
671200 67 1189
671300 67 2322
671400 67 1693
671500 67 3569
671600 67 2617
671800 67 1216
671900 67 1303
672000 67 3677
834900 67 2549
680500 68 1601
680600 68 1362
680900 68 3925
681000 68 2942
681100 68 3549
681200 68 2867
681300 68 2742
681400 68 2775
834600 68 3110
834700 68 1980
834800 68 1971
690300 69 2220
690400 69 3530
690500 69 1288
690900 69 4410
691000 69 2614
691100 69 3159
691200 69 2645
691300 69 2721
691400 69 3432
691500 69 2021
841800 69 2379
842500 69 2454
700100 70 3206
700200 70 6798
700301 70 5349
700302 70 5377
700401 70 6256
700402 70 4306
700501 70 8373
700502 70 3123
710100 71 1392
710200 71 5926
710300 71 1562
710400 71 5098
710500 71 4439
710600 71 1996
710700 71 2858
710800 71 4657
710900 71 2756
711000 71 3473
711100 71 3599
711200 71 4920
711300 71 1742
711400 71 2493
711500 71 2723
720100 72 4090
720200 72 4594
720300 72 3547
720400 72 2185
720500 72 2150
720600 72 1670
720700 72 2990
730100 73 2567
730201 73 6039
730202 73 3230
730300 73 1185
730400 73 3596
730500 73 4257
730600 73 3147
730700 73 2000
740100 74 2962
740200 74 6472
740300 74 4413
740400 74 4628
750100 75 4516
750200 75 2846
750300 75 2758
750400 75 3162
750500 75 5582
750600 75 3817
823304 75 0
760801 76 4390
760802 76 2173
760803 76 6977
770500 76 0
770602 76 0
770700 76 0
770800 76 0
770902 76 147
980000 76 0
030101 77 3794
030102 77 3139
030103 77 1881
030104 77 3580
030200 77 5721
030300 77 2830
030400 77 2558
030500 77 6533
030601 77 3256
030603 77 2223
030604 77 2796
030701 77 1814
030702 77 1554
030703 77 2783
030706 77 3290
030800 77 4496
030900 77 3085
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment