Spaces:
Sleeping
Sleeping
cassiebuhler
commited on
Commit
•
be7a922
1
Parent(s):
b1c508f
adding states
Browse files- get_party.ipynb +170 -260
get_party.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"id": "d179ded1-6235-47ed-bbfb-6d72468188d5",
|
7 |
"metadata": {},
|
8 |
"outputs": [],
|
@@ -30,31 +30,10 @@
|
|
30 |
},
|
31 |
{
|
32 |
"cell_type": "code",
|
33 |
-
"execution_count":
|
34 |
"id": "ab644102-c725-4cf4-915c-8550a0a74c32",
|
35 |
"metadata": {},
|
36 |
-
"outputs": [
|
37 |
-
{
|
38 |
-
"name": "stdout",
|
39 |
-
"output_type": "stream",
|
40 |
-
"text": [
|
41 |
-
" year key current_party previous_party\n",
|
42 |
-
"0 2004 AUTAUGA COUNTY-AL REPUBLICAN REPUBLICAN\n",
|
43 |
-
"1 2004 COVINGTON COUNTY-AL REPUBLICAN REPUBLICAN\n",
|
44 |
-
"2 2004 ELMORE COUNTY-AL REPUBLICAN REPUBLICAN\n",
|
45 |
-
"3 2004 HENRY COUNTY-AL REPUBLICAN REPUBLICAN\n",
|
46 |
-
"4 2004 LOWNDES COUNTY-AL DEMOCRAT DEMOCRAT\n",
|
47 |
-
"... ... ... ... ...\n",
|
48 |
-
"14904 2020 ELK COUNTY-PA REPUBLICAN REPUBLICAN\n",
|
49 |
-
"14905 2020 LOVING COUNTY-TX REPUBLICAN REPUBLICAN\n",
|
50 |
-
"14906 2020 RED RIVER COUNTY-TX REPUBLICAN REPUBLICAN\n",
|
51 |
-
"14907 2020 WHATCOM COUNTY-WA DEMOCRAT DEMOCRAT\n",
|
52 |
-
"14908 2020 DISTRICT 12 COUNTY-AK REPUBLICAN REPUBLICAN\n",
|
53 |
-
"\n",
|
54 |
-
"[14909 rows x 4 columns]\n"
|
55 |
-
]
|
56 |
-
}
|
57 |
-
],
|
58 |
"source": [
|
59 |
"filtered = county.filter((_.mode == \"TOTAL\") & (_.totalvotes > 0))\n",
|
60 |
"\n",
|
@@ -102,7 +81,7 @@
|
|
102 |
},
|
103 |
{
|
104 |
"cell_type": "code",
|
105 |
-
"execution_count":
|
106 |
"id": "ce0d80bf-3b78-4aa9-8048-5cc0dbf970d9",
|
107 |
"metadata": {},
|
108 |
"outputs": [],
|
@@ -118,7 +97,7 @@
|
|
118 |
},
|
119 |
{
|
120 |
"cell_type": "code",
|
121 |
-
"execution_count":
|
122 |
"id": "87bef5e2-a40a-4aff-aa27-e7d49ec68aac",
|
123 |
"metadata": {},
|
124 |
"outputs": [
|
@@ -177,7 +156,7 @@
|
|
177 |
},
|
178 |
{
|
179 |
"cell_type": "code",
|
180 |
-
"execution_count":
|
181 |
"id": "b086e76c-4285-4036-8033-e4e45cb6966b",
|
182 |
"metadata": {},
|
183 |
"outputs": [],
|
@@ -265,240 +244,10 @@
|
|
265 |
},
|
266 |
{
|
267 |
"cell_type": "code",
|
268 |
-
"execution_count":
|
269 |
"id": "b3429fea-7c0d-4838-bcbb-6552079dc3b6",
|
270 |
"metadata": {},
|
271 |
-
"outputs": [
|
272 |
-
{
|
273 |
-
"data": {
|
274 |
-
"text/html": [
|
275 |
-
"<div>\n",
|
276 |
-
"<style scoped>\n",
|
277 |
-
" .dataframe tbody tr th:only-of-type {\n",
|
278 |
-
" vertical-align: middle;\n",
|
279 |
-
" }\n",
|
280 |
-
"\n",
|
281 |
-
" .dataframe tbody tr th {\n",
|
282 |
-
" vertical-align: top;\n",
|
283 |
-
" }\n",
|
284 |
-
"\n",
|
285 |
-
" .dataframe thead th {\n",
|
286 |
-
" text-align: right;\n",
|
287 |
-
" }\n",
|
288 |
-
"</style>\n",
|
289 |
-
"<table border=\"1\" class=\"dataframe\">\n",
|
290 |
-
" <thead>\n",
|
291 |
-
" <tr style=\"text-align: right;\">\n",
|
292 |
-
" <th></th>\n",
|
293 |
-
" <th>key</th>\n",
|
294 |
-
" <th>geometry</th>\n",
|
295 |
-
" <th>Status</th>\n",
|
296 |
-
" <th>yes</th>\n",
|
297 |
-
" <th>year</th>\n",
|
298 |
-
" <th>amount</th>\n",
|
299 |
-
" <th>log_amount</th>\n",
|
300 |
-
" <th>jurisdiction</th>\n",
|
301 |
-
" <th>current_party</th>\n",
|
302 |
-
" <th>previous_party</th>\n",
|
303 |
-
" </tr>\n",
|
304 |
-
" </thead>\n",
|
305 |
-
" <tbody>\n",
|
306 |
-
" <tr>\n",
|
307 |
-
" <th>0</th>\n",
|
308 |
-
" <td>San Luis Obispo-CA</td>\n",
|
309 |
-
" <td>POLYGON ((-120.58962 35.28275, -120.59096 35.2...</td>\n",
|
310 |
-
" <td>Pass</td>\n",
|
311 |
-
" <td>58.23%</td>\n",
|
312 |
-
" <td>2020</td>\n",
|
313 |
-
" <td>$21,600,000</td>\n",
|
314 |
-
" <td>16.8882</td>\n",
|
315 |
-
" <td>Municipal</td>\n",
|
316 |
-
" <td>DEMOCRAT</td>\n",
|
317 |
-
" <td>DEMOCRAT</td>\n",
|
318 |
-
" </tr>\n",
|
319 |
-
" <tr>\n",
|
320 |
-
" <th>1</th>\n",
|
321 |
-
" <td>Fruita-CO</td>\n",
|
322 |
-
" <td>POLYGON ((-108.65899 39.15887, -108.66033 39.1...</td>\n",
|
323 |
-
" <td>Pass</td>\n",
|
324 |
-
" <td>60.73%</td>\n",
|
325 |
-
" <td>2020</td>\n",
|
326 |
-
" <td>$1,300,000</td>\n",
|
327 |
-
" <td>14.0779</td>\n",
|
328 |
-
" <td>Municipal</td>\n",
|
329 |
-
" <td>REPUBLICAN</td>\n",
|
330 |
-
" <td>REPUBLICAN</td>\n",
|
331 |
-
" </tr>\n",
|
332 |
-
" <tr>\n",
|
333 |
-
" <th>2</th>\n",
|
334 |
-
" <td>Gig Harbor-WA</td>\n",
|
335 |
-
" <td>POLYGON ((-122.51787 47.32597, -122.51921 47.3...</td>\n",
|
336 |
-
" <td>Pass</td>\n",
|
337 |
-
" <td>62.05%</td>\n",
|
338 |
-
" <td>2004</td>\n",
|
339 |
-
" <td>$3,500,000</td>\n",
|
340 |
-
" <td>15.0683</td>\n",
|
341 |
-
" <td>Municipal</td>\n",
|
342 |
-
" <td>DEMOCRAT</td>\n",
|
343 |
-
" <td>DEMOCRAT</td>\n",
|
344 |
-
" </tr>\n",
|
345 |
-
" <tr>\n",
|
346 |
-
" <th>3</th>\n",
|
347 |
-
" <td>Jupiter-FL</td>\n",
|
348 |
-
" <td>POLYGON ((-80.02421 26.93422, -80.02555 26.920...</td>\n",
|
349 |
-
" <td>Pass</td>\n",
|
350 |
-
" <td>70.84%</td>\n",
|
351 |
-
" <td>2004</td>\n",
|
352 |
-
" <td>$17,000,000</td>\n",
|
353 |
-
" <td>16.6487</td>\n",
|
354 |
-
" <td>Municipal</td>\n",
|
355 |
-
" <td>DEMOCRAT</td>\n",
|
356 |
-
" <td>DEMOCRAT</td>\n",
|
357 |
-
" </tr>\n",
|
358 |
-
" <tr>\n",
|
359 |
-
" <th>4</th>\n",
|
360 |
-
" <td>Bozeman-MT</td>\n",
|
361 |
-
" <td>POLYGON ((-110.97405 45.67943, -110.97539 45.6...</td>\n",
|
362 |
-
" <td>Pass</td>\n",
|
363 |
-
" <td>73.39%</td>\n",
|
364 |
-
" <td>2012</td>\n",
|
365 |
-
" <td>$7,500,000</td>\n",
|
366 |
-
" <td>15.8304</td>\n",
|
367 |
-
" <td>Municipal</td>\n",
|
368 |
-
" <td>REPUBLICAN</td>\n",
|
369 |
-
" <td>DEMOCRAT</td>\n",
|
370 |
-
" </tr>\n",
|
371 |
-
" <tr>\n",
|
372 |
-
" <th>...</th>\n",
|
373 |
-
" <td>...</td>\n",
|
374 |
-
" <td>...</td>\n",
|
375 |
-
" <td>...</td>\n",
|
376 |
-
" <td>...</td>\n",
|
377 |
-
" <td>...</td>\n",
|
378 |
-
" <td>...</td>\n",
|
379 |
-
" <td>...</td>\n",
|
380 |
-
" <td>...</td>\n",
|
381 |
-
" <td>...</td>\n",
|
382 |
-
" <td>...</td>\n",
|
383 |
-
" </tr>\n",
|
384 |
-
" <tr>\n",
|
385 |
-
" <th>268</th>\n",
|
386 |
-
" <td>Chandler-AZ</td>\n",
|
387 |
-
" <td>POLYGON ((-111.77118 33.3062, -111.77253 33.29...</td>\n",
|
388 |
-
" <td>Pass</td>\n",
|
389 |
-
" <td>75.16%</td>\n",
|
390 |
-
" <td>2004</td>\n",
|
391 |
-
" <td>$17,350,000</td>\n",
|
392 |
-
" <td>16.6691</td>\n",
|
393 |
-
" <td>Municipal</td>\n",
|
394 |
-
" <td>REPUBLICAN</td>\n",
|
395 |
-
" <td>REPUBLICAN</td>\n",
|
396 |
-
" </tr>\n",
|
397 |
-
" <tr>\n",
|
398 |
-
" <th>269</th>\n",
|
399 |
-
" <td>Medfield-MA</td>\n",
|
400 |
-
" <td>POLYGON ((-71.23546 42.18656, -71.23681 42.172...</td>\n",
|
401 |
-
" <td>Pass</td>\n",
|
402 |
-
" <td>84.17%</td>\n",
|
403 |
-
" <td>2008</td>\n",
|
404 |
-
" <td>$1,550,000</td>\n",
|
405 |
-
" <td>14.2538</td>\n",
|
406 |
-
" <td>Municipal</td>\n",
|
407 |
-
" <td>DEMOCRAT</td>\n",
|
408 |
-
" <td>DEMOCRAT</td>\n",
|
409 |
-
" </tr>\n",
|
410 |
-
" <tr>\n",
|
411 |
-
" <th>270</th>\n",
|
412 |
-
" <td>Somerville-MA</td>\n",
|
413 |
-
" <td>POLYGON ((-71.0295 42.3876, -71.03084 42.37394...</td>\n",
|
414 |
-
" <td>Pass</td>\n",
|
415 |
-
" <td>76.07%</td>\n",
|
416 |
-
" <td>2012</td>\n",
|
417 |
-
" <td>$9,659,333</td>\n",
|
418 |
-
" <td>16.0834</td>\n",
|
419 |
-
" <td>Municipal</td>\n",
|
420 |
-
" <td>DEMOCRAT</td>\n",
|
421 |
-
" <td>DEMOCRAT</td>\n",
|
422 |
-
" </tr>\n",
|
423 |
-
" <tr>\n",
|
424 |
-
" <th>271</th>\n",
|
425 |
-
" <td>Dryden-NY</td>\n",
|
426 |
-
" <td>POLYGON ((-76.22716 42.49091, -76.2285 42.4772...</td>\n",
|
427 |
-
" <td>Fail</td>\n",
|
428 |
-
" <td>44.07%</td>\n",
|
429 |
-
" <td>2016</td>\n",
|
430 |
-
" <td>$71,800</td>\n",
|
431 |
-
" <td>11.1816</td>\n",
|
432 |
-
" <td>Municipal</td>\n",
|
433 |
-
" <td>DEMOCRAT</td>\n",
|
434 |
-
" <td>DEMOCRAT</td>\n",
|
435 |
-
" </tr>\n",
|
436 |
-
" <tr>\n",
|
437 |
-
" <th>272</th>\n",
|
438 |
-
" <td>Grafton-WI</td>\n",
|
439 |
-
" <td>POLYGON ((-87.88178 43.31952, -87.88313 43.305...</td>\n",
|
440 |
-
" <td>Fail</td>\n",
|
441 |
-
" <td>28.78%</td>\n",
|
442 |
-
" <td>2004</td>\n",
|
443 |
-
" <td>$1,120,000</td>\n",
|
444 |
-
" <td>13.9288</td>\n",
|
445 |
-
" <td>Municipal</td>\n",
|
446 |
-
" <td>REPUBLICAN</td>\n",
|
447 |
-
" <td>REPUBLICAN</td>\n",
|
448 |
-
" </tr>\n",
|
449 |
-
" </tbody>\n",
|
450 |
-
"</table>\n",
|
451 |
-
"<p>273 rows × 10 columns</p>\n",
|
452 |
-
"</div>"
|
453 |
-
],
|
454 |
-
"text/plain": [
|
455 |
-
" key geometry \\\n",
|
456 |
-
"0 San Luis Obispo-CA POLYGON ((-120.58962 35.28275, -120.59096 35.2... \n",
|
457 |
-
"1 Fruita-CO POLYGON ((-108.65899 39.15887, -108.66033 39.1... \n",
|
458 |
-
"2 Gig Harbor-WA POLYGON ((-122.51787 47.32597, -122.51921 47.3... \n",
|
459 |
-
"3 Jupiter-FL POLYGON ((-80.02421 26.93422, -80.02555 26.920... \n",
|
460 |
-
"4 Bozeman-MT POLYGON ((-110.97405 45.67943, -110.97539 45.6... \n",
|
461 |
-
".. ... ... \n",
|
462 |
-
"268 Chandler-AZ POLYGON ((-111.77118 33.3062, -111.77253 33.29... \n",
|
463 |
-
"269 Medfield-MA POLYGON ((-71.23546 42.18656, -71.23681 42.172... \n",
|
464 |
-
"270 Somerville-MA POLYGON ((-71.0295 42.3876, -71.03084 42.37394... \n",
|
465 |
-
"271 Dryden-NY POLYGON ((-76.22716 42.49091, -76.2285 42.4772... \n",
|
466 |
-
"272 Grafton-WI POLYGON ((-87.88178 43.31952, -87.88313 43.305... \n",
|
467 |
-
"\n",
|
468 |
-
" Status yes year amount log_amount jurisdiction current_party \\\n",
|
469 |
-
"0 Pass 58.23% 2020 $21,600,000 16.8882 Municipal DEMOCRAT \n",
|
470 |
-
"1 Pass 60.73% 2020 $1,300,000 14.0779 Municipal REPUBLICAN \n",
|
471 |
-
"2 Pass 62.05% 2004 $3,500,000 15.0683 Municipal DEMOCRAT \n",
|
472 |
-
"3 Pass 70.84% 2004 $17,000,000 16.6487 Municipal DEMOCRAT \n",
|
473 |
-
"4 Pass 73.39% 2012 $7,500,000 15.8304 Municipal REPUBLICAN \n",
|
474 |
-
".. ... ... ... ... ... ... ... \n",
|
475 |
-
"268 Pass 75.16% 2004 $17,350,000 16.6691 Municipal REPUBLICAN \n",
|
476 |
-
"269 Pass 84.17% 2008 $1,550,000 14.2538 Municipal DEMOCRAT \n",
|
477 |
-
"270 Pass 76.07% 2012 $9,659,333 16.0834 Municipal DEMOCRAT \n",
|
478 |
-
"271 Fail 44.07% 2016 $71,800 11.1816 Municipal DEMOCRAT \n",
|
479 |
-
"272 Fail 28.78% 2004 $1,120,000 13.9288 Municipal REPUBLICAN \n",
|
480 |
-
"\n",
|
481 |
-
" previous_party \n",
|
482 |
-
"0 DEMOCRAT \n",
|
483 |
-
"1 REPUBLICAN \n",
|
484 |
-
"2 DEMOCRAT \n",
|
485 |
-
"3 DEMOCRAT \n",
|
486 |
-
"4 DEMOCRAT \n",
|
487 |
-
".. ... \n",
|
488 |
-
"268 REPUBLICAN \n",
|
489 |
-
"269 DEMOCRAT \n",
|
490 |
-
"270 DEMOCRAT \n",
|
491 |
-
"271 DEMOCRAT \n",
|
492 |
-
"272 REPUBLICAN \n",
|
493 |
-
"\n",
|
494 |
-
"[273 rows x 10 columns]"
|
495 |
-
]
|
496 |
-
},
|
497 |
-
"execution_count": 15,
|
498 |
-
"metadata": {},
|
499 |
-
"output_type": "execute_result"
|
500 |
-
}
|
501 |
-
],
|
502 |
"source": [
|
503 |
"\n",
|
504 |
"localities_boundaries = \"us_localities.parquet\"\n",
|
@@ -556,11 +305,172 @@
|
|
556 |
"# hf_upload(\"municipal_parties.pmtiles\", \"boettiger-lab/landvote\")\n"
|
557 |
]
|
558 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
559 |
{
|
560 |
"cell_type": "code",
|
561 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
562 |
"id": "322b9a85-bdf9-45f9-9b19-695cc1b996e8",
|
563 |
"metadata": {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
"outputs": [],
|
565 |
"source": []
|
566 |
}
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 16,
|
6 |
"id": "d179ded1-6235-47ed-bbfb-6d72468188d5",
|
7 |
"metadata": {},
|
8 |
"outputs": [],
|
|
|
30 |
},
|
31 |
{
|
32 |
"cell_type": "code",
|
33 |
+
"execution_count": null,
|
34 |
"id": "ab644102-c725-4cf4-915c-8550a0a74c32",
|
35 |
"metadata": {},
|
36 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
"source": [
|
38 |
"filtered = county.filter((_.mode == \"TOTAL\") & (_.totalvotes > 0))\n",
|
39 |
"\n",
|
|
|
81 |
},
|
82 |
{
|
83 |
"cell_type": "code",
|
84 |
+
"execution_count": null,
|
85 |
"id": "ce0d80bf-3b78-4aa9-8048-5cc0dbf970d9",
|
86 |
"metadata": {},
|
87 |
"outputs": [],
|
|
|
97 |
},
|
98 |
{
|
99 |
"cell_type": "code",
|
100 |
+
"execution_count": 30,
|
101 |
"id": "87bef5e2-a40a-4aff-aa27-e7d49ec68aac",
|
102 |
"metadata": {},
|
103 |
"outputs": [
|
|
|
156 |
},
|
157 |
{
|
158 |
"cell_type": "code",
|
159 |
+
"execution_count": null,
|
160 |
"id": "b086e76c-4285-4036-8033-e4e45cb6966b",
|
161 |
"metadata": {},
|
162 |
"outputs": [],
|
|
|
244 |
},
|
245 |
{
|
246 |
"cell_type": "code",
|
247 |
+
"execution_count": null,
|
248 |
"id": "b3429fea-7c0d-4838-bcbb-6552079dc3b6",
|
249 |
"metadata": {},
|
250 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
"source": [
|
252 |
"\n",
|
253 |
"localities_boundaries = \"us_localities.parquet\"\n",
|
|
|
305 |
"# hf_upload(\"municipal_parties.pmtiles\", \"boettiger-lab/landvote\")\n"
|
306 |
]
|
307 |
},
|
308 |
+
{
|
309 |
+
"cell_type": "markdown",
|
310 |
+
"id": "06e24a7e-5f7f-42bc-b515-43082016d496",
|
311 |
+
"metadata": {},
|
312 |
+
"source": [
|
313 |
+
"# Get States"
|
314 |
+
]
|
315 |
+
},
|
316 |
{
|
317 |
"cell_type": "code",
|
318 |
+
"execution_count": 28,
|
319 |
+
"id": "217170ca-b732-4875-b4cb-f8a8cd2fc405",
|
320 |
+
"metadata": {},
|
321 |
+
"outputs": [],
|
322 |
+
"source": [
|
323 |
+
"states = (conn\n",
|
324 |
+
" .read_csv(\"1976-2020-president.csv\")\n",
|
325 |
+
" .filter(_. year >=2000)\n",
|
326 |
+
" )\n",
|
327 |
+
"# states.execute()"
|
328 |
+
]
|
329 |
+
},
|
330 |
+
{
|
331 |
+
"cell_type": "code",
|
332 |
+
"execution_count": 18,
|
333 |
"id": "322b9a85-bdf9-45f9-9b19-695cc1b996e8",
|
334 |
"metadata": {},
|
335 |
+
"outputs": [
|
336 |
+
{
|
337 |
+
"name": "stdout",
|
338 |
+
"output_type": "stream",
|
339 |
+
"text": [
|
340 |
+
" year key current_party previous_party\n",
|
341 |
+
"0 2004 HI DEMOCRAT DEMOCRAT\n",
|
342 |
+
"1 2004 ME DEMOCRAT DEMOCRAT\n",
|
343 |
+
"2 2004 NJ DEMOCRAT DEMOCRAT\n",
|
344 |
+
"3 2004 NM REPUBLICAN DEMOCRAT\n",
|
345 |
+
"4 2004 ND REPUBLICAN REPUBLICAN\n",
|
346 |
+
".. ... .. ... ...\n",
|
347 |
+
"250 2020 VT DEMOCRAT DEMOCRAT\n",
|
348 |
+
"251 2020 AL REPUBLICAN REPUBLICAN\n",
|
349 |
+
"252 2020 IA REPUBLICAN REPUBLICAN\n",
|
350 |
+
"253 2020 SD REPUBLICAN REPUBLICAN\n",
|
351 |
+
"254 2020 GA DEMOCRAT REPUBLICAN\n",
|
352 |
+
"\n",
|
353 |
+
"[255 rows x 4 columns]\n"
|
354 |
+
]
|
355 |
+
}
|
356 |
+
],
|
357 |
+
"source": [
|
358 |
+
"# filtered = county.filter((_.mode == \"TOTAL\") & (_.totalvotes > 0))\n",
|
359 |
+
"\n",
|
360 |
+
"# Find the winning party for each year, state, and county\n",
|
361 |
+
"most_votes= (\n",
|
362 |
+
" states\n",
|
363 |
+
" .group_by(['year', 'state_po', 'party_simplified'])\n",
|
364 |
+
" .aggregate(winning_votes=_.candidatevotes.sum())\n",
|
365 |
+
")\n",
|
366 |
+
"\n",
|
367 |
+
"# For each year, state, and county, select the party with the highest total votes\n",
|
368 |
+
"winning_party = (\n",
|
369 |
+
" most_votes\n",
|
370 |
+
" .group_by('year', 'state_po')\n",
|
371 |
+
" .aggregate(\n",
|
372 |
+
" max_votes=_.winning_votes.max(), # Max votes in this group\n",
|
373 |
+
" )\n",
|
374 |
+
" .join(\n",
|
375 |
+
" most_votes,\n",
|
376 |
+
" [\"year\",\"state_po\",most_votes['winning_votes'] == _.max_votes],\n",
|
377 |
+
" how='inner'\n",
|
378 |
+
" )\n",
|
379 |
+
" .select(\"year\",\"state_po\",most_votes['party_simplified'].name('current_party')\n",
|
380 |
+
" )\n",
|
381 |
+
")\n",
|
382 |
+
"\n",
|
383 |
+
"# Self-join to get the previous year's winning party\n",
|
384 |
+
"previous_year = winning_party.view()\n",
|
385 |
+
"\n",
|
386 |
+
"joined = (\n",
|
387 |
+
" winning_party\n",
|
388 |
+
" .join(\n",
|
389 |
+
" previous_year, [\"state_po\",winning_party['year'] == previous_year['year'] + 4],\n",
|
390 |
+
" how='left'\n",
|
391 |
+
" )\n",
|
392 |
+
" .rename(key = \"state_po\")\n",
|
393 |
+
" # .mutate(key = _.county_name + ibis.literal(\" COUNTY-\") + _.state_id)\n",
|
394 |
+
" .select(\"year\",\"key\",\"current_party\",previous_year['current_party'].name('previous_party'))\n",
|
395 |
+
")\n",
|
396 |
+
"\n",
|
397 |
+
"state_parties = joined.filter(_.year >2000).order_by(\"year\")\n",
|
398 |
+
"\n",
|
399 |
+
"print(state_parties.execute())"
|
400 |
+
]
|
401 |
+
},
|
402 |
+
{
|
403 |
+
"cell_type": "code",
|
404 |
+
"execution_count": 31,
|
405 |
+
"id": "2c03920e-76da-4034-8eaf-1e80a56f5b0d",
|
406 |
+
"metadata": {},
|
407 |
+
"outputs": [
|
408 |
+
{
|
409 |
+
"data": {
|
410 |
+
"application/vnd.jupyter.widget-view+json": {
|
411 |
+
"model_id": "3574af6546ff4cd1949e27c63cd15cd7",
|
412 |
+
"version_major": 2,
|
413 |
+
"version_minor": 0
|
414 |
+
},
|
415 |
+
"text/plain": [
|
416 |
+
"states_parties.parquet: 0%| | 0.00/2.36M [00:00<?, ?B/s]"
|
417 |
+
]
|
418 |
+
},
|
419 |
+
"metadata": {},
|
420 |
+
"output_type": "display_data"
|
421 |
+
},
|
422 |
+
{
|
423 |
+
"name": "stderr",
|
424 |
+
"output_type": "stream",
|
425 |
+
"text": [
|
426 |
+
"For layer 0, using name \"states_parties\"\n",
|
427 |
+
"12 features, 833591 bytes of geometry and attributes, 542 bytes of string pool, 0 bytes of vertices, 0 bytes of nodes\n",
|
428 |
+
"Choosing a maxzoom of -z0 for features typically 7514540 feet (2290432 meters) apart, and at least 2073685 feet (632060 meters) apart\n",
|
429 |
+
"Choosing a maxzoom of -z10 for resolution of about 376 feet (114 meters) within features\n",
|
430 |
+
" 99.9% 10/271/383 \n",
|
431 |
+
" 100.0% 10/187/380 \r"
|
432 |
+
]
|
433 |
+
},
|
434 |
+
{
|
435 |
+
"name": "stdout",
|
436 |
+
"output_type": "stream",
|
437 |
+
"text": [
|
438 |
+
"Successfully generated PMTiles file: states_parties.pmtiles\n"
|
439 |
+
]
|
440 |
+
}
|
441 |
+
],
|
442 |
+
"source": [
|
443 |
+
"# state_boundaries = \"https://data.source.coop/cboettig/us-boundaries/us-state-territory.parquet\"\n",
|
444 |
+
"states = conn.read_parquet(\"vote_states.parquet\")\n",
|
445 |
+
"\n",
|
446 |
+
"df_states = (states\n",
|
447 |
+
" .mutate(key = _.key.upper())\n",
|
448 |
+
" # .filter(_.jurisdiction == \"Municipal\")\n",
|
449 |
+
" .join(state_parties, [\"key\",\"year\"],how='inner'\n",
|
450 |
+
" )\n",
|
451 |
+
" .cast({\"geometry\": \"geometry\"})\n",
|
452 |
+
")\n",
|
453 |
+
"\n",
|
454 |
+
"gdf_states = df_states.execute()\n",
|
455 |
+
"gdf_states = gdf_states.set_crs(\"EPSG:4326\")\n",
|
456 |
+
"gdf_states\n",
|
457 |
+
"\n",
|
458 |
+
"\n",
|
459 |
+
"gdf_states.to_parquet(\"states_parties.parquet\")\n",
|
460 |
+
"hf_upload(\"states_parties.parquet\", \"boettiger-lab/landvote\")\n",
|
461 |
+
"\n",
|
462 |
+
"gdf_states.to_file(\"states_parties.geojson\")\n",
|
463 |
+
"hf_upload(\"states_parties.geojson\", \"boettiger-lab/landvote\")\n",
|
464 |
+
"\n",
|
465 |
+
"generate_pmtiles(\"states_parties.geojson\", \"states_parties.pmtiles\")\n",
|
466 |
+
"hf_upload(\"states_parties.pmtiles\", \"boettiger-lab/landvote\")\n"
|
467 |
+
]
|
468 |
+
},
|
469 |
+
{
|
470 |
+
"cell_type": "code",
|
471 |
+
"execution_count": null,
|
472 |
+
"id": "dcf5d049-68ae-4d73-be77-72d985c9ed1c",
|
473 |
+
"metadata": {},
|
474 |
"outputs": [],
|
475 |
"source": []
|
476 |
}
|