lab07-checkpoint.ipynb 354 KB
Newer Older
root's avatar
root committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab 07 - Data Preprocessing 2\n",
    "\n",
    "Name: Mitch Gavars and Meg Koczur\n",
    "\n",
    "Class: CSCI 349 \n",
    "\n",
    "Semester: 2021 Spring\n",
    "\n",
    "Insructor: Brian King"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1) [P] Create a Python function called process_FAA_hourly_data that takes a filename (with path) as a string, and returns a completely processed pandas data frame of the data, ready for analysis. It should do everything that the previous lab did to clean and prepare the file, including\n",
    "a. converting all numeric variables to their simplest numeric types\n",
    "\n",
    "b. converting the date/time stamp (first variable) to a pandas DatetimeIndex, which becomes the\n",
    "actual index for the data frame.\n",
    "\n",
    "c. It should drop the date time variable after moving it to become the index.\n",
    "\n",
    "d. If you did not do this in the last lab, make sure that the DatetimeIndex is localized to a specific\n",
    "timezone! This is very important! What time zone? Did you notice the header? The time stamp is in GMT, so be sure to localize the index accordingly. HOW? After you set up the index, you can do:\n",
    "\n",
    "##### df.index = df.index.tz_localize(tz='GMT')\n",
    "\n",
    "NOTE: The last exercise in the previous lab had you eliminate a year from the data for the very last\n",
    "problem. Do NOT do that here! We'll explore that again later."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_FAA_hourly_data( file_path, header_rows ):\n",
    "    #path = '../data/faa_hourly-KIPT_20000101-20201231_raw.csv'\n",
    "    df = pd.read_csv(file_path, skiprows = header_rows)\n",
    "    \n",
    "    # a. converting all numeric variables to their simplest numeric types\n",
    "    for i in df.columns[2:]:\n",
    "        if df[i].dtype == np.dtype('float64'):\n",
    "            df[i] = pd.to_numeric(df[i], downcast = 'float')\n",
    "        \n",
    "    # b. converting the date/time stamp (first variable) to a pandas DatetimeIndex, which becomes the actual index for the data frame.\n",
    "    df.iloc[:,0] = pd.to_datetime(df.iloc[:,0])\n",
    "    \n",
    "    # c. It should drop the date time variable after moving it to become the index.\n",
    "    df = df.set_index( df.iloc[:,0] )\n",
    "    df = df.drop(columns = [\"Date/Time (GMT)\"])\n",
    "\n",
    "    # d. d. If you did not do this in the last lab, make sure that the DatetimeIndex is localized to a specific timezone!\n",
    "    df.index = df.index.tz_localize(tz='GMT')\n",
    "    \n",
    "    return df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2) [P] Use your new function to read in the KIPT data file you downloaded in the last lab. Store your data frame as df_kipt. Output the results of info() and describe() to confirm you read it in correctly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "DatetimeIndex: 181943 entries, 2000-01-01 00:00:00+00:00 to 2020-12-31 23:00:00+00:00\n",
      "Data columns (total 13 columns):\n",
      " #   Column                         Non-Null Count   Dtype  \n",
      "---  ------                         --------------   -----  \n",
      " 0   Number of Observations (n/a)   181943 non-null  int64  \n",
      " 1   Average Temp (F)               180938 non-null  float32\n",
      " 2   Max Temp (F)                   180938 non-null  float32\n",
      " 3   Min Temp (F)                   180938 non-null  float32\n",
      " 4   Average Dewpoint Temp (F)      180816 non-null  float32\n",
      " 5   1 Hour Precip (in)             30294 non-null   float32\n",
      " 6   Max Wind Gust (mph)            24708 non-null   float32\n",
      " 7   Average Relative Humidity (%)  177114 non-null  float32\n",
      " 8   Average Wind Speed (mph)       181394 non-null  float32\n",
      " 9   Average Station Pressure (mb)  181647 non-null  float32\n",
      " 10  Average Wind Direction (deg)   148822 non-null  float32\n",
      " 11  Max Wind Speed (mph)           181394 non-null  float32\n",
      " 12  Unnamed: 13                    0 non-null       float32\n",
      "dtypes: float32(12), int64(1)\n",
      "memory usage: 11.1 MB\n"
     ]
    }
   ],
   "source": [
    "df_kipt = process_FAA_hourly_data( '../data/faa_hourly-KIPT_20000101-20201231_raw.csv', 16 )\n",
    "df_kipt.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Observations (n/a)</th>\n",
       "      <th>Average Temp (F)</th>\n",
       "      <th>Max Temp (F)</th>\n",
       "      <th>Min Temp (F)</th>\n",
       "      <th>Average Dewpoint Temp (F)</th>\n",
       "      <th>1 Hour Precip (in)</th>\n",
       "      <th>Max Wind Gust (mph)</th>\n",
       "      <th>Average Relative Humidity (%)</th>\n",
       "      <th>Average Wind Speed (mph)</th>\n",
       "      <th>Average Station Pressure (mb)</th>\n",
       "      <th>Average Wind Direction (deg)</th>\n",
       "      <th>Max Wind Speed (mph)</th>\n",
       "      <th>Unnamed: 13</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>181943.000000</td>\n",
       "      <td>180938.000000</td>\n",
       "      <td>180938.000000</td>\n",
       "      <td>180938.000000</td>\n",
       "      <td>180816.000000</td>\n",
       "      <td>30294.000000</td>\n",
       "      <td>24708.000000</td>\n",
       "      <td>177114.000000</td>\n",
       "      <td>181394.000000</td>\n",
       "      <td>181647.000000</td>\n",
       "      <td>148822.000000</td>\n",
       "      <td>181394.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.336990</td>\n",
       "      <td>51.373848</td>\n",
       "      <td>51.485367</td>\n",
       "      <td>51.268284</td>\n",
       "      <td>40.278069</td>\n",
       "      <td>0.030406</td>\n",
       "      <td>22.367750</td>\n",
       "      <td>68.680298</td>\n",
       "      <td>5.908508</td>\n",
       "      <td>1016.748840</td>\n",
       "      <td>175.468781</td>\n",
       "      <td>6.177359</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.851021</td>\n",
       "      <td>18.850498</td>\n",
       "      <td>18.869318</td>\n",
       "      <td>18.842798</td>\n",
       "      <td>18.965900</td>\n",
       "      <td>0.078684</td>\n",
       "      <td>7.490209</td>\n",
       "      <td>19.678192</td>\n",
       "      <td>5.188278</td>\n",
       "      <td>7.636273</td>\n",
       "      <td>119.225632</td>\n",
       "      <td>5.303607</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>-11.900000</td>\n",
       "      <td>-11.900000</td>\n",
       "      <td>-11.900000</td>\n",
       "      <td>-20.900000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>508.600006</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>19.600000</td>\n",
       "      <td>54.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1012.200012</td>\n",
       "      <td>70.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>51.799999</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>21.900000</td>\n",
       "      <td>71.000000</td>\n",
       "      <td>5.400000</td>\n",
       "      <td>1016.900024</td>\n",
       "      <td>210.000000</td>\n",
       "      <td>5.800000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>66.900002</td>\n",
       "      <td>66.900002</td>\n",
       "      <td>66.900002</td>\n",
       "      <td>57.000000</td>\n",
       "      <td>0.030000</td>\n",
       "      <td>26.500000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>9.200000</td>\n",
       "      <td>1021.700012</td>\n",
       "      <td>280.000000</td>\n",
       "      <td>9.200000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>10.000000</td>\n",
       "      <td>102.000000</td>\n",
       "      <td>102.000000</td>\n",
       "      <td>102.000000</td>\n",
       "      <td>79.000000</td>\n",
       "      <td>2.350000</td>\n",
       "      <td>88.599998</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>76.000000</td>\n",
       "      <td>1044.400024</td>\n",
       "      <td>360.000000</td>\n",
       "      <td>76.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Number of Observations (n/a)  Average Temp (F)   Max Temp (F)  \\\n",
       "count                 181943.000000     180938.000000  180938.000000   \n",
       "mean                       1.336990         51.373848      51.485367   \n",
       "std                        0.851021         18.850498      18.869318   \n",
       "min                        0.000000        -11.900000     -11.900000   \n",
       "25%                        1.000000         36.000000      36.000000   \n",
       "50%                        1.000000         52.000000      52.000000   \n",
       "75%                        1.000000         66.900002      66.900002   \n",
       "max                       10.000000        102.000000     102.000000   \n",
       "\n",
       "        Min Temp (F)  Average Dewpoint Temp (F)  1 Hour Precip (in)  \\\n",
       "count  180938.000000              180816.000000        30294.000000   \n",
       "mean       51.268284                  40.278069            0.030406   \n",
       "std        18.842798                  18.965900            0.078684   \n",
       "min       -11.900000                 -20.900000            0.000000   \n",
       "25%        36.000000                  26.100000            0.000000   \n",
       "50%        51.799999                  41.000000            0.000000   \n",
       "75%        66.900002                  57.000000            0.030000   \n",
       "max       102.000000                  79.000000            2.350000   \n",
       "\n",
       "       Max Wind Gust (mph)  Average Relative Humidity (%)  \\\n",
       "count         24708.000000                  177114.000000   \n",
       "mean             22.367750                      68.680298   \n",
       "std               7.490209                      19.678192   \n",
       "min               0.000000                       0.000000   \n",
       "25%              19.600000                      54.000000   \n",
       "50%              21.900000                      71.000000   \n",
       "75%              26.500000                      86.000000   \n",
       "max              88.599998                     100.000000   \n",
       "\n",
       "       Average Wind Speed (mph)  Average Station Pressure (mb)  \\\n",
       "count             181394.000000                  181647.000000   \n",
       "mean                   5.908508                    1016.748840   \n",
       "std                    5.188278                       7.636273   \n",
       "min                    0.000000                     508.600006   \n",
       "25%                    0.000000                    1012.200012   \n",
       "50%                    5.400000                    1016.900024   \n",
       "75%                    9.200000                    1021.700012   \n",
       "max                   76.000000                    1044.400024   \n",
       "\n",
       "       Average Wind Direction (deg)  Max Wind Speed (mph)  Unnamed: 13  \n",
       "count                 148822.000000         181394.000000          0.0  \n",
       "mean                     175.468781              6.177359          NaN  \n",
       "std                      119.225632              5.303607          NaN  \n",
       "min                        0.000000              0.000000          NaN  \n",
       "25%                       70.000000              0.000000          NaN  \n",
       "50%                      210.000000              5.800000          NaN  \n",
       "75%                      280.000000              9.200000          NaN  \n",
       "max                      360.000000             76.000000          NaN  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_kipt.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3) [P] In the last lab, you assessed the number of missing dates in your data, under the assumption that every hour should have an observation. For now, we'll ignore the fact that there are completely missing hourly observations from the weather station. Report the number of missing values in each variable of df_kipt from the data you have."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Number of Observations (n/a)          0\n",
       "Average Temp (F)                   1005\n",
       "Max Temp (F)                       1005\n",
       "Min Temp (F)                       1005\n",
       "Average Dewpoint Temp (F)          1127\n",
       "1 Hour Precip (in)               151649\n",
       "Max Wind Gust (mph)              157235\n",
       "Average Relative Humidity (%)      4829\n",
       "Average Wind Speed (mph)            549\n",
       "Average Station Pressure (mb)       296\n",
       "Average Wind Direction (deg)      33121\n",
       "Max Wind Speed (mph)                549\n",
       "Unnamed: 13                      181943\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_kipt[ df_kipt.columns ].isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 4) [P/M] Let's pay attention to \"Average Temp (F)\". Are there hours of the day are most likely to have missing values? Report the frequency over each hour that has missing \"Average Temp (F)\" values. Be sure to report the LOCAL times according to the time zone \"US/Eastern\". Output the hours in order of the most frequently missing to least. Then, as a comment, interpret your findings. Do you see a pattern? Do missing temps tend to happen at a certain time of day?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "hour\n",
       "22      115\n",
       "6       112\n",
       "4       110\n",
       "8       110\n",
       "9       105\n",
       "21      101\n",
       "5       101\n",
       "23       93\n",
       "7        93\n",
       "15       93\n",
       "3        90\n",
       "10       89\n",
       "11       87\n",
       "16       86\n",
       "14       85\n",
       "17       85\n",
       "12       82\n",
       "18       81\n",
       "19       78\n",
       "1        78\n",
       "0        77\n",
       "13       74\n",
       "20       70\n",
       "2        66\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "miss_times = pd.date_range(start=df_kipt.index[0], end=df_kipt.index[-1], freq=\"60min\", closed='left').difference(df_kipt.index)\n",
    "df_missing = pd.DataFrame(miss_times.hour, index=miss_times, columns = [\"hour\"] )\n",
    "df_missing.value_counts()\n",
    "\n",
    "# There does not seem to be a strong outlier in terms of most missing hours, but the best time range for missing temps is about 5-9am"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 5) [P/M] Repeat the previous exercise, but this time, assess the same variable for the day of the week. (NOTE: Be sure to note what a 0 is. In pandas, a 0 for day of the week is a Monday! (See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DatetimeIndex.dayofweek.html )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "week\n",
       "3       375\n",
       "0       324\n",
       "6       319\n",
       "4       314\n",
       "2       288\n",
       "5       283\n",
       "1       258\n",
       "dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "miss_times = pd.date_range(start=df_kipt.index[0], end=df_kipt.index[-1], freq=\"60min\", closed='left').difference(df_kipt.index)\n",
    "df_missing = pd.DataFrame(miss_times.weekday, index=miss_times, columns = [\"week\"] )\n",
    "df_missing.value_counts()\n",
    "\n",
    "# Thursday seems to be the day of the week with a large amount of missing times compared to the next frequency (Monday). \n",
    "# However, there does not seem to be a pattern between the days of the week"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 6) [P] Read in the file FAA_PA_stations.csv provided on Moodle. It's not actually a comma separated file, but a tab separated file. Store the data frame as stations. Show stations.info() after you read in the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 46 entries, 0 to 45\n",
      "Data columns (total 7 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   ID                46 non-null     object \n",
      " 1   Name              46 non-null     object \n",
      " 2   County            45 non-null     object \n",
      " 3   State             46 non-null     object \n",
      " 4   Lat               46 non-null     float64\n",
      " 5   Lon               46 non-null     float64\n",
      " 6   Elevation (feet)  46 non-null     float64\n",
      "dtypes: float64(3), object(4)\n",
      "memory usage: 2.6+ KB\n"
     ]
    }
   ],
   "source": [
    "stations = pd.read_csv('../data/FAA_PA_stations_raw.csv', sep='\\t')\n",
    "stations.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 7) [P] As usual, you must always assess your missing data, if any. Are there any observations (rows) in stations that have missing data? Output them, then eliminate them from your data. Be sure toreset_index(drop=True) to reset the index in case any observations are dropped. Output stations.info() again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID                            KUKT\n",
      "Name                QUAKERTOWN ARP\n",
      "County                         NaN\n",
      "State                           PA\n",
      "Lat                         40.435\n",
      "Lon                        -75.381\n",
      "Elevation (feet)             524.9\n",
      "Name: 24, dtype: object \n",
      "\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 45 entries, 0 to 44\n",
      "Data columns (total 7 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   ID                45 non-null     object \n",
      " 1   Name              45 non-null     object \n",
      " 2   County            45 non-null     object \n",
      " 3   State             45 non-null     object \n",
      " 4   Lat               45 non-null     float64\n",
      " 5   Lon               45 non-null     float64\n",
      " 6   Elevation (feet)  45 non-null     float64\n",
      "dtypes: float64(3), object(4)\n",
      "memory usage: 2.6+ KB\n"
     ]
    }
   ],
   "source": [
    "for ind, series in stations.iterrows():\n",
    "    if series.isna().any():\n",
    "        print(series, '\\n')\n",
    "        stations = stations.drop(index=ind)\n",
    "        stations = stations.reset_index(drop=True)\n",
    "\n",
    "stations.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 8) [P] Examine the data frame of stations by showing the first few observations using stations.head(10) In particular, pay close attention to the variables Lat and Lon. These represent the precise latitude and longitude geolocation for the weather station."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Name</th>\n",
       "      <th>County</th>\n",
       "      <th>State</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Elevation (feet)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>KABE</td>\n",
       "      <td>ALLENTOWN</td>\n",
       "      <td>LEHIGH</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.65</td>\n",
       "      <td>-75.44</td>\n",
       "      <td>376.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>KAOO</td>\n",
       "      <td>ALTOONA</td>\n",
       "      <td>BLAIR</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.29</td>\n",
       "      <td>-78.32</td>\n",
       "      <td>1504.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>KBVI</td>\n",
       "      <td>BEAVER FALLS</td>\n",
       "      <td>BEAVER</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.77</td>\n",
       "      <td>-80.39</td>\n",
       "      <td>1230.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>KBFD</td>\n",
       "      <td>BRADFORD</td>\n",
       "      <td>MCKEAN</td>\n",
       "      <td>PA</td>\n",
       "      <td>41.80</td>\n",
       "      <td>-78.64</td>\n",
       "      <td>2142.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>KBTP</td>\n",
       "      <td>BUTLER</td>\n",
       "      <td>BUTLER</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.77</td>\n",
       "      <td>-79.95</td>\n",
       "      <td>1250.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>KCXY</td>\n",
       "      <td>CAPITAL CITY</td>\n",
       "      <td>YORK</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.22</td>\n",
       "      <td>-76.85</td>\n",
       "      <td>340.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>KFIG</td>\n",
       "      <td>CLEARFIELD</td>\n",
       "      <td>CLEARFIELD</td>\n",
       "      <td>PA</td>\n",
       "      <td>41.04</td>\n",
       "      <td>-78.41</td>\n",
       "      <td>1516.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>KDYL</td>\n",
       "      <td>DOYLESTOWN</td>\n",
       "      <td>BUCKS</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.33</td>\n",
       "      <td>-75.12</td>\n",
       "      <td>394.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>KDUJ</td>\n",
       "      <td>DUBOIS</td>\n",
       "      <td>JEFFERSON</td>\n",
       "      <td>PA</td>\n",
       "      <td>41.18</td>\n",
       "      <td>-78.90</td>\n",
       "      <td>1814.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>KERI</td>\n",
       "      <td>ERIE</td>\n",
       "      <td>ERIE</td>\n",
       "      <td>PA</td>\n",
       "      <td>42.08</td>\n",
       "      <td>-80.17</td>\n",
       "      <td>730.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ID          Name      County State    Lat    Lon  Elevation (feet)\n",
       "0  KABE     ALLENTOWN      LEHIGH    PA  40.65 -75.44             376.0\n",
       "1  KAOO       ALTOONA       BLAIR    PA  40.29 -78.32            1504.0\n",
       "2  KBVI  BEAVER FALLS      BEAVER    PA  40.77 -80.39            1230.0\n",
       "3  KBFD      BRADFORD      MCKEAN    PA  41.80 -78.64            2142.0\n",
       "4  KBTP        BUTLER      BUTLER    PA  40.77 -79.95            1250.0\n",
       "5  KCXY  CAPITAL CITY        YORK    PA  40.22 -76.85             340.0\n",
       "6  KFIG    CLEARFIELD  CLEARFIELD    PA  41.04 -78.41            1516.0\n",
       "7  KDYL    DOYLESTOWN       BUCKS    PA  40.33 -75.12             394.0\n",
       "8  KDUJ        DUBOIS   JEFFERSON    PA  41.18 -78.90            1814.0\n",
       "9  KERI          ERIE        ERIE    PA  42.08 -80.17             730.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stations.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 9) Create a new variable in stations called \"distKIPT\" that stores the distance of every station in PA to Williamsport (KIPT). Use a standard Euclidean distance calculation (over latitude and longitude) to compute the distance between the stations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Name</th>\n",
       "      <th>County</th>\n",
       "      <th>State</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Elevation (feet)</th>\n",
       "      <th>distKIPT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>KABE</td>\n",
       "      <td>ALLENTOWN</td>\n",
       "      <td>LEHIGH</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.65</td>\n",
       "      <td>-75.44</td>\n",
       "      <td>376.0</td>\n",
       "      <td>8972.336202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>KAOO</td>\n",
       "      <td>ALTOONA</td>\n",
       "      <td>BLAIR</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.29</td>\n",
       "      <td>-78.32</td>\n",
       "      <td>1504.0</td>\n",
       "      <td>9319.940762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>KBVI</td>\n",
       "      <td>BEAVER FALLS</td>\n",
       "      <td>BEAVER</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.77</td>\n",
       "      <td>-80.39</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>12410.807110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>KBFD</td>\n",
       "      <td>BRADFORD</td>\n",
       "      <td>MCKEAN</td>\n",
       "      <td>PA</td>\n",
       "      <td>41.80</td>\n",
       "      <td>-78.64</td>\n",
       "      <td>2142.0</td>\n",
       "      <td>7856.283823</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>KBTP</td>\n",
       "      <td>BUTLER</td>\n",
       "      <td>BUTLER</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.77</td>\n",
       "      <td>-79.95</td>\n",
       "      <td>1250.0</td>\n",
       "      <td>12551.942324</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ID          Name  County State    Lat    Lon  Elevation (feet)  \\\n",
       "0  KABE     ALLENTOWN  LEHIGH    PA  40.65 -75.44             376.0   \n",
       "1  KAOO       ALTOONA   BLAIR    PA  40.29 -78.32            1504.0   \n",
       "2  KBVI  BEAVER FALLS  BEAVER    PA  40.77 -80.39            1230.0   \n",
       "3  KBFD      BRADFORD  MCKEAN    PA  41.80 -78.64            2142.0   \n",
       "4  KBTP        BUTLER  BUTLER    PA  40.77 -79.95            1250.0   \n",
       "\n",
       "       distKIPT  \n",
       "0   8972.336202  \n",
       "1   9319.940762  \n",
       "2  12410.807110  \n",
       "3   7856.283823  \n",
       "4  12551.942324  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# x = R * cos(lat) * cos(lon)\n",
    "# y = R * cos(lat) * sin(lon)\n",
    "# z = R * sin(lat)\n",
    "# https://math.stackexchange.com/questions/29157/how-do-i-convert-the-distance-between-two-lat-long-points-into-feet-meters\n",
    "import math\n",
    "\n",
    "kipt_lat = stations.at[29,\"Lat\"]\n",
    "kipt_lon = stations.at[29,\"Lon\"]\n",
    "R = 6371\n",
    "\n",
    "kipt_x = R * math.cos(kipt_lat) * math.cos(kipt_lon)\n",
    "kipt_y = R * math.cos(kipt_lat) * math.sin(kipt_lon)\n",
    "kipt_z = R * math.sin(kipt_lat)\n",
    "\n",
    "def distToKIPT(lat,lon):\n",
    "    x = R * math.cos(lat) * math.cos(lon)\n",
    "    y = R * math.cos(lat) * math.sin(lon)\n",
    "    z = R * math.sin(lat)\n",
    "    \n",
    "    dist = math.sqrt( math.pow( (x-kipt_x),2 ) +  math.pow( (y-kipt_y),2 ) + math.pow( (z-kipt_z),2 ) )\n",
    "\n",
    "    return dist\n",
    "\n",
    "distances = []\n",
    "for ind,series in stations.iterrows():\n",
    "    distances.append( distToKIPT( series.loc[\"Lat\"] , series.loc[\"Lon\"] ) )\n",
    "    \n",
    "stations['distKIPT'] = distances\n",
    "stations.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 10) [P] Output the top 10 stations that are closest to KIPT. (The closest one should be to itself!) The stations should be listed in order of increasing distance from KIPT."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Name</th>\n",
       "      <th>County</th>\n",
       "      <th>State</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Elevation (feet)</th>\n",
       "      <th>distKIPT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>KIPT</td>\n",
       "      <td>WILLIAMSPORT</td>\n",
       "      <td>LYCOMING</td>\n",
       "      <td>PA</td>\n",
       "      <td>41.24</td>\n",
       "      <td>-76.92</td>\n",
       "      <td>520.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>KSEG</td>\n",
       "      <td>SELINSGROVE</td>\n",
       "      <td>SNYDER</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.82</td>\n",
       "      <td>-76.86</td>\n",
       "      <td>444.0</td>\n",
       "      <td>2681.405634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>KMUI</td>\n",
       "      <td>MUIR ARMY AIR FIELD</td>\n",
       "      <td>LEBANON</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.43</td>\n",
       "      <td>-76.57</td>\n",
       "      <td>489.0</td>\n",
       "      <td>5418.830052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>KUNV</td>\n",
       "      <td>UNIVERSITY PARK</td>\n",
       "      <td>CENTRE</td>\n",
       "      <td>PA</td>\n",
       "      <td>40.85</td>\n",
       "      <td>-77.85</td>\n",
       "      <td>1240.0</td>\n",
       "      <td>6014.471034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>KCXY</td>\n",