File size: 60,546 Bytes
f1da5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6a85f28c",
   "metadata": {},
   "source": [
    "# Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bb5aaa8",
   "metadata": {},
   "source": [
    "This is the script for processing the huggingface dataset \"zwn22/NC_Crime\"."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "356beb0a",
   "metadata": {},
   "source": [
    "# Durham "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f31470ef",
   "metadata": {},
   "source": [
    "NC State Plane ESPG: 2264 https://epsg.io/2264"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "ae2c8d75",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "Durham = pd.read_excel('Durham.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaee9c05",
   "metadata": {},
   "source": [
    "## Exploratory Data Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "16c2b839",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Case Number', 'Report Date', 'Report Time', 'Status', 'Sequence',\n",
       "       'ATT/COM', 'UCR Code', 'Offense', 'Address', 'X', 'Y', 'District',\n",
       "       'Beat', 'Tract', 'Premise', 'Weapon'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Durham.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6059809c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['(blank)', 'Not Applicable/None', 'Unknown/Not Stated',\n",
       "       'Personal Weapons', 'Knife/Cutting Instrument', 'Handgun',\n",
       "       'Blunt Objects', 'Rifle', 'Asphyxiation', 'Other Weapon',\n",
       "       'Narcotics/Drugs', 'Undetermined Firearm', 'Shotgun',\n",
       "       'Motor Vehicle/Vessel', 'Fire/Burning Tool/Device',\n",
       "       'Other Firearm', 'Explosives', 'Poison'], dtype=object)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Durham['Weapon'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e836498c",
   "metadata": {},
   "outputs": [],
   "source": [
    "Durham['Weapon'] = Durham['Weapon'].replace(['(blank)', 'Not Applicable/None', 'Unknown/Not Stated'], None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a484806d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['INTIMIDATION', 'FRAUD-IDENTITY THEFT',\n",
       "       'LARCENY - AUTOMOBILE PARTS OR ACCESSORIES',\n",
       "       'TOWED/ABANDONED VEHICLE', 'DRUG/NARCOTIC VIOLATIONS',\n",
       "       'DRUG EQUIPMENT/PARAPHERNALIA', 'MOTOR VEHICLE THEFT', 'BURGLARY',\n",
       "       'SIMPLE ASSAULT', 'LARCENY - FROM MOTOR VEHICLE',\n",
       "       'LARCENY - SHOPLIFTING', 'LOST PROPERTY', 'VANDALISM',\n",
       "       'LARCENY - ALL OTHER', 'DV INFO REPORT (NO CRIME)',\n",
       "       'ALL OTHER CRIMINAL OFFENSES', 'LARCENY - FROM BUILDING',\n",
       "       'ALL OTHER OFFENSES-PAROLE/PROBATION VIOLATIONS', 'EMBEZZLEMENT',\n",
       "       'ASSIST OTHER AGENCY', 'AGGRAVATED ASSAULT',\n",
       "       'SEX OFFENSE - FORCIBLE RAPE',\n",
       "       'SEX OFFENSE - SEXUAL ASSAULT WITH AN OBJECT',\n",
       "       'COUNTERFEITING/FORGERY', 'FRAUD - CONFIDENCE GAMES/TRICKERY',\n",
       "       'SUSPICIOUS ACTIVITY', 'CALLS FOR SERVICE (NO CRIME)',\n",
       "       'ROBBERY - COMMERCIAL', 'ROBBERY - INDIVIDUAL',\n",
       "       'FRAUD - CREDIT CARD/ATM',\n",
       "       'FRAUD - UNAUTHORIZED USE OF CONVEYANCE', 'RUNAWAY',\n",
       "       'RECOVERED STOLEN PROPERTY (OTHER JURISDICTION)',\n",
       "       'ALL OTHER OFFENSES-COURT VIOLATIONS', 'DEATH INVESTIGATION',\n",
       "       'FRAUD - FALSE PRETENSE', 'SUICIDE', 'ALL TRAFFIC (EXCEPT DWI)',\n",
       "       'RECOVERED STOLEN VEHICLE (OTHER JURISDICTION)', 'TRESPASSING',\n",
       "       'FRAUD - IMPERSONATION', 'KIDNAPPING/ABDUCTION',\n",
       "       'WEAPON VIOLATIONS', 'ALL OTHER OFFENSES - HARASSING PHONE CALLS',\n",
       "       'FOUND PROPERTY', 'FRAUD - WIRE/COMPUTER/OTHER ELECTRONIC',\n",
       "       'FRAUD - WORTHLESS CHECKS', 'OFFENSE AGAINST FAMILY - OTHER',\n",
       "       'NON-CRIMINAL DETAINMENT (INVOLUNTARY COMMITMENT)',\n",
       "       'SEX OFFENSE - FONDLING', 'UNDISCIPLINED JUVENILE',\n",
       "       'LIQUOR LAW VIOLATIONS', 'BLACKMAIL/EXTORTION',\n",
       "       'DRIVING WHILE IMPAIRED', 'CRIME SCENE INVESTIGATION',\n",
       "       'MISSING PERSON', 'STOLEN PROPERTY',\n",
       "       'HOMICIDE-MURDER/NON-NEGLIGENT MANSLAUGHTER',\n",
       "       'DISORDERLY CONDUCT-DRUNK AND DISRUPTIVE',\n",
       "       'OFFENSES AGAINST FAMILY - CHILD ABUSE',\n",
       "       'ALL OTHER OFFENSES-ESCAPE FROM CUSTODY OR RESIST ARREST',\n",
       "       'CURFEW/LOITERING/VAGRANCY VIOLATIONS',\n",
       "       'SEX OFFENSE - FORCIBLE SODOMY',\n",
       "       'ALL OTHER OFFENSES-CITY ORDINANCE VIOLATIONS',\n",
       "       'PORNOGRAPHY/OBSCENE MATERIAL', 'ARSON', 'DISORDERLY CONDUCT',\n",
       "       'OFFENSES AGAINST FAMILY - NEGLECT',\n",
       "       'SEX OFFENSE - INDECENT EXPOSURE',\n",
       "       'DISORDERLY CONDUCT-FIGHTING (AFFRAY)', 'ROBBERY - BANK',\n",
       "       'LARCENY - POCKET-PICKING', 'LARCENY - FROM COIN-OPERATED DEVICE',\n",
       "       'FRAUD - WELFARE FRAUD', 'ANIMAL CRUELTY',\n",
       "       'SEX OFFENSE - STATUTORY RAPE', 'LARCENY - PURSESNATCHING',\n",
       "       'PROSTITUTION', 'FRAUD-FAIL TO RETURN RENTAL VEHICLE',\n",
       "       'OFFENSES AGAINST FAMILY - DESERTION/ABANDONMENT', 'BRIBERY',\n",
       "       'SEX OFFENSE - PEEPING TOM', 'JUSTIFIABLE HOMICIDE',\n",
       "       'FRAUD-HACKING/COMPUTER INVASION', 'TRUANCY',\n",
       "       'HUMAN TRAFFICKING/INVOLUNTARY SERVITUDE',\n",
       "       'HOMICIDE - NEGLIGENT MANSLAUGHTER',\n",
       "       'RESIST ARREST, ETC-REPEALED DO NOT USE',\n",
       "       'VANDALISM TO AUTO (NOT ACCIDENTAL)',\n",
       "       'DOMESTIC VIOLENCE ORDER VIOL-REPEALED DO NOT USE',\n",
       "       'GAMBLING - OPERATING/PROMOTING/ASSISTING',\n",
       "       'PROSTITUTION - ASSISTING/PROMOTING', 'SEX OFFENSE - INCEST',\n",
       "       'GAMBLING - BETTING/WAGERING', 'PROSTITUTION - PURCHASING',\n",
       "       'DISORDERLY CONDUCT-UNLAWFUL ASSEMBLY',\n",
       "       'HUMAN TRAFFICKING/COMMERCIAL SEX ACTS'], dtype=object)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Durham['Offense'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "da37a48b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyproj import Transformer\n",
    "\n",
    "def convert_coordinates(x, y):\n",
    "\n",
    "    transformer = Transformer.from_crs(\"epsg:2264\", \"epsg:4326\", always_xy=True)  # 注意设置always_xy=True以保持x,y顺序\n",
    "\n",
    "    lon, lat = transformer.transform(x, y)\n",
    "    \n",
    "    return pd.Series([lat, lon])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "eed49097",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a mapping dictionary for crime categories\n",
    "category_mapping = {\n",
    "    'Theft': ['LARCENY - AUTOMOBILE PARTS OR ACCESSORIES', 'TOWED/ABANDONED VEHICLE', 'MOTOR VEHICLE THEFT', 'BURGLARY', 'LARCENY - FROM MOTOR VEHICLE', 'LARCENY - SHOPLIFTING', 'LOST PROPERTY', 'VANDALISM', 'LARCENY - ALL OTHER', 'LARCENY - FROM BUILDING', 'RECOVERED STOLEN PROPERTY (OTHER JURISDICTION)', 'LARCENY - POCKET-PICKING', 'LARCENY - FROM COIN-OPERATED DEVICE', 'LARCENY - PURSESNATCHING'],\n",
    "    'Fraud': ['FRAUD-IDENTITY THEFT', 'EMBEZZLEMENT', 'COUNTERFEITING/FORGERY', 'FRAUD - CONFIDENCE GAMES/TRICKERY', 'FRAUD - CREDIT CARD/ATM', 'FRAUD - UNAUTHORIZED USE OF CONVEYANCE', 'FRAUD - FALSE PRETENSE', 'FRAUD - IMPERSONATION', 'FRAUD - WIRE/COMPUTER/OTHER ELECTRONIC', 'FRAUD - WORTHLESS CHECKS', 'FRAUD-FAIL TO RETURN RENTAL VEHICLE', 'FRAUD-HACKING/COMPUTER INVASION', 'FRAUD-WELFARE FRAUD'],\n",
    "    'Assault': ['SIMPLE ASSAULT', 'AGGRAVATED ASSAULT'],\n",
    "    'Drugs': ['DRUG/NARCOTIC VIOLATIONS', 'DRUG EQUIPMENT/PARAPHERNALIA'],\n",
    "    'Sexual Offenses': ['SEX OFFENSE - FORCIBLE RAPE', 'SEX OFFENSE - SEXUAL ASSAULT WITH AN OBJECT', 'SEX OFFENSE - FONDLING', 'SEX OFFENSE - INDECENT EXPOSURE', 'SEX OFFENSE - FORCIBLE SODOMY', 'SEX OFFENSE - STATUTORY RAPE', 'SEX OFFENSE - PEEPING TOM', 'SEX OFFENSE - INCEST'],\n",
    "    'Homicide': ['HOMICIDE-MURDER/NON-NEGLIGENT MANSLAUGHTER', 'JUSTIFIABLE HOMICIDE', 'HOMICIDE - NEGLIGENT MANSLAUGHTER'],\n",
    "    'Arson': ['ARSON'],\n",
    "    'Kidnapping': ['KIDNAPPING/ABDUCTION'],\n",
    "    'Weapons Violations': ['WEAPON VIOLATIONS'],\n",
    "    'Traffic Violations': ['ALL TRAFFIC (EXCEPT DWI)'],\n",
    "    'Disorderly Conduct': ['DISORDERLY CONDUCT', 'DISORDERLY CONDUCT-DRUNK AND DISRUPTIVE', 'DISORDERLY CONDUCT-FIGHTING (AFFRAY)', 'DISORDERLY CONDUCT-UNLAWFUL ASSEMBLY'],\n",
    "    'Gambling': ['GAMBLING - OPERATING/PROMOTING/ASSISTING', 'GAMBLING - BETTING/WAGERING'],\n",
    "    'Animal-related Offenses': ['ANIMAL CRUELTY'],\n",
    "    'Prostitution-related Offenses': ['PROSTITUTION', 'PROSTITUTION - ASSISTING/PROMOTING', 'PROSTITUTION - PURCHASING']\n",
    "}\n",
    "\n",
    "# Function to categorize crime based on the mapping dictionary\n",
    "def categorize_crime(crime):\n",
    "    for category, crimes in category_mapping.items():\n",
    "        if crime in crimes:\n",
    "            return category\n",
    "    return 'Miscellaneous'\n",
    "\n",
    "# Create a new DataFrame with simplified crime categories\n",
    "Durham_new = pd.DataFrame({\n",
    "    \"year\": pd.to_datetime(Durham['Report Date']).dt.year,\n",
    "    \"city\": \"Durham\",\n",
    "    \"crime_major_category\": Durham['Offense'].apply(categorize_crime),\n",
    "    \"crime_detail\": Durham['Offense'].str.title(),\n",
    "    \"latitude\": Durham['X'],\n",
    "    \"longitude\": Durham['Y'],\n",
    "    \"occurance_time\": pd.to_datetime(Durham['Report Date'] + ' ' + Durham['Report Time']).dt.strftime('%Y/%m/%d %H:%M:%S'),\n",
    "    \"clear_status\": Durham['Status'],\n",
    "    \"incident_address\": Durham['Address'],\n",
    "    \"notes\": Durham['Weapon'].apply(lambda x: f\"Weapon: {x}\" if pd.notnull(x) else \"No Data\")\n",
    "})\n",
    "\n",
    "Durham_new[['latitude', 'longitude']] = Durham.apply(lambda row: convert_coordinates(row['X'], row['Y']), axis=1).round(5).fillna(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "c8d699cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "Durham_new = Durham_new[Durham_new['year'] >= 2015].fillna(\"No Data\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "fe3be70e",
   "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>year</th>\n",
       "      <th>city</th>\n",
       "      <th>crime_major_category</th>\n",
       "      <th>crime_detail</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>occurance_time</th>\n",
       "      <th>clear_status</th>\n",
       "      <th>incident_address</th>\n",
       "      <th>notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>149919</th>\n",
       "      <td>2022</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Theft</td>\n",
       "      <td>Larceny - From Motor Vehicle</td>\n",
       "      <td>35.88024</td>\n",
       "      <td>-78.85024</td>\n",
       "      <td>2022/04/13 00:15:00</td>\n",
       "      <td>Cleared By Arrest</td>\n",
       "      <td>5400 S MIAMI BLVD</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149920</th>\n",
       "      <td>2022</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Recovered Stolen Vehicle (Other Jurisdiction)</td>\n",
       "      <td>35.88037</td>\n",
       "      <td>-78.85057</td>\n",
       "      <td>2022/04/13 00:15:00</td>\n",
       "      <td>Cleared By Arrest</td>\n",
       "      <td>5400 S MIAMI BLVD</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149921</th>\n",
       "      <td>2022</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Assault</td>\n",
       "      <td>Aggravated Assault</td>\n",
       "      <td>35.96519</td>\n",
       "      <td>-78.94559</td>\n",
       "      <td>2022/12/10 01:55:00</td>\n",
       "      <td>Cleared By Exception</td>\n",
       "      <td>3200 OLD CHAPEL HILL RD</td>\n",
       "      <td>Weapon: Handgun</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149922</th>\n",
       "      <td>2022</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Theft</td>\n",
       "      <td>Vandalism</td>\n",
       "      <td>35.88496</td>\n",
       "      <td>-78.84567</td>\n",
       "      <td>2022/12/31 00:00:00</td>\n",
       "      <td>Active/Open</td>\n",
       "      <td>100 TATUM DR</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149923</th>\n",
       "      <td>2022</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Fraud</td>\n",
       "      <td>Fraud - Credit Card/Atm</td>\n",
       "      <td>35.99019</td>\n",
       "      <td>-78.89111</td>\n",
       "      <td>2022/05/03 08:36:00</td>\n",
       "      <td>Cleared By Arrest</td>\n",
       "      <td>800 E MAIN ST</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        year    city crime_major_category  \\\n",
       "149919  2022  Durham                Theft   \n",
       "149920  2022  Durham        Miscellaneous   \n",
       "149921  2022  Durham              Assault   \n",
       "149922  2022  Durham                Theft   \n",
       "149923  2022  Durham                Fraud   \n",
       "\n",
       "                                         crime_detail  latitude  longitude  \\\n",
       "149919                   Larceny - From Motor Vehicle  35.88024  -78.85024   \n",
       "149920  Recovered Stolen Vehicle (Other Jurisdiction)  35.88037  -78.85057   \n",
       "149921                             Aggravated Assault  35.96519  -78.94559   \n",
       "149922                                      Vandalism  35.88496  -78.84567   \n",
       "149923                        Fraud - Credit Card/Atm  35.99019  -78.89111   \n",
       "\n",
       "             occurance_time          clear_status         incident_address  \\\n",
       "149919  2022/04/13 00:15:00     Cleared By Arrest        5400 S MIAMI BLVD   \n",
       "149920  2022/04/13 00:15:00     Cleared By Arrest        5400 S MIAMI BLVD   \n",
       "149921  2022/12/10 01:55:00  Cleared By Exception  3200 OLD CHAPEL HILL RD   \n",
       "149922  2022/12/31 00:00:00           Active/Open             100 TATUM DR   \n",
       "149923  2022/05/03 08:36:00     Cleared By Arrest            800 E MAIN ST   \n",
       "\n",
       "                  notes  \n",
       "149919  Weapon: (blank)  \n",
       "149920  Weapon: (blank)  \n",
       "149921  Weapon: Handgun  \n",
       "149922  Weapon: (blank)  \n",
       "149923  Weapon: (blank)  "
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Durham_new.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "55fc0352",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(149922, 10)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Durham_new.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a87d3c7",
   "metadata": {},
   "source": [
    "# Chapel Hill"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "b6052404",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "Chapel = pd.read_csv(\"Chapel_hill.csv\", low_memory=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "502ffe3b",
   "metadata": {},
   "source": [
    "## Exploratory Data Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "00878dd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['X', 'Y', 'Incident_ID', 'Agency', 'Offense', 'Street', 'City', 'State',\n",
       "       'Zipcode', 'Date_of_Report', 'Date_of_Occurrence', 'Date_Found',\n",
       "       'Reported_As', 'Premise_Description', 'Forcible', 'Weapon_Description',\n",
       "       'Victim_Age', 'Victim_Race', 'Victim_Gender', 'Latitude', 'Longitude',\n",
       "       'ObjectId'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Chapel.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "30789db9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['<Null>', 'DISTURBANCE/NUI', 'SUSPICIOUS/WANT', 'TRAFFIC STOP',\n",
       "       'THEFT/LARCENY', 'MISC OFFICER IN', 'BURGLARY/HOME I',\n",
       "       'INDECENCY/LEWDN', 'TRAFFIC/TRANSPO', 'MVC W INJURY',\n",
       "       'DAMAGE/VANDALIS', 'PUBLIC SERVICE', 'TRESPASSING/UNW',\n",
       "       'INFO MESSAGE', 'HARASSMENT/STAL', 'DOMESTIC DISTUR',\n",
       "       'ADMINISTRATIVE', 'ELECTRICAL HAZA', 'CARDIAC ARREST',\n",
       "       'FRAUD OR DECEPT', 'ASSIST CITIZEN', 'ASSAULT/SEXUAL',\n",
       "       'INTOXICATED SUB', 'LE ASSISTANCE', 'DRUGS', 'SUSPICIOUS OR W',\n",
       "       'ARREST', 'DISPUTE', 'DISTURBANCE', 'BURGLARY', 'ASSIST OTHR AGE',\n",
       "       'LARCENY FROM AU', 'TRESPASSING', 'DAMAGE TO PROPE',\n",
       "       'REFUSAL TO LEAV', 'UNKNOWN PROBLEM', 'WEAPON/FIREARMS',\n",
       "       'LOUD NOISE', 'ESCORT', 'ABDUCTION/CUSTO', 'VANDALISM',\n",
       "       'LARCENY OF OTHE', 'LARCENY FROM PE', 'THREATS', 'LARCENY FROM BU',\n",
       "       'BURGLAR ALARM', 'DOMESTIC', 'CARDIAC RESP AR', 'PROPERTY FOUND',\n",
       "       'ASSAULT', 'FIREWORKS', 'MISSING/RUNAWAY', 'OVERDOSE',\n",
       "       'SEXUAL OFFENSE', 'MENTAL DISORDER', 'CHECK WELL BEIN',\n",
       "       'SUSPICIOUS COND', 'PSYCHIATRIC', 'OPEN DOOR', 'ABANDONED AUTO',\n",
       "       'HARASSMENT THRE', 'TRAFFIC VIOLATI', 'ANIMAL BITE',\n",
       "       'LARCENY OF BIKE', 'SOLICITATION', 'JUVENILE RELATE',\n",
       "       'ASSIST MOTORIST', 'ANIMAL', 'ANIMAL CALL', 'HAZARDOUS DRIVI',\n",
       "       'LARCENY FROM RE', 'LARCENY OF AUTO', 'MVC', 'SUICIDE ATTEMPT',\n",
       "       'GAS LEAK FIRE', 'MISSING PERSON', 'ASSIST OTHER AG',\n",
       "       'PUBLICE SERVICE', 'DOMESTIC ASSIST', 'BURGLARY ATTEMP',\n",
       "       'SUSPICIOUS VEHI', 'STAB GUNSHOT PE', 'UNKNOWN LE',\n",
       "       'ROBBERY/CARJACK', 'MOTOR VEHICLE C', 'ALARMS', '911 HANGUP',\n",
       "       'STRUCTURE FIRE', 'ABUSE/ABANDOMEN', 'VEHICLE FIRE', 'EXPLOSION',\n",
       "       'DECEASED PERSON', 'DRIVING UNDER I', 'GUNSHOT INJURY',\n",
       "       'SCHOOL PATROL', 'ACTIVE ASSAILAN', 'BOMB/CBRN/PRODU',\n",
       "       'STATIONARY PATR', 'LITTERING', 'HOUSE CHECK', 'STAB GSW OR PEN',\n",
       "       'CARDIAC', 'CLOSE PATROL', 'BOMB FOUND/SUSP', 'STRUCTURE COLLA',\n",
       "       'INFO FOR ALL UN', 'MVC ENTRAPMENT', 'UNCONCIOUS OR F',\n",
       "       'LIFTING ASSISTA', 'ATTEMPT TO LOCA', 'SICK PERSON',\n",
       "       'HEAT OR COLD EX', 'CONFINED SPACE', 'TRAUMATIC INJUR',\n",
       "       'MVC W INJURY AB', 'MVC W INJURY DE', 'DROWNING', 'CITY ORDINANCE'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Chapel['Reported_As'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "a31a68f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['<Null>', 'HANDGUN', 'UNKNOWN', 'PERSONAL WEAPONS', 'NONE',\n",
       "       'SHOTGUN', 'OTHER FIREARM', 'OTHER', 'RIFLE', 'UNARMED',\n",
       "       'FIREARM (TYP NOT STATED)', 'KNIFE/CUTTING INSTRUMENT',\n",
       "       'DRUGS/NARCOTICS/SLEEPING PILLS', 'BLUNT OBJECT',\n",
       "       'FIREARM (TYPE NOT STATED)', 'MOTOR VEHICLE',\n",
       "       'FIRE/INCENDIARY DEVICE', 'POISON', 'EXPLOSIVES', nan,\n",
       "       'ASPHYXIATION'], dtype=object)"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Chapel['Weapon_Description'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "10ccc474",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Replace specified values with None\n",
    "replace_values = {'<Null>': None, 'NONE': None}\n",
    "Chapel['Weapon_Description'] = Chapel['Weapon_Description'].replace(replace_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "f3bca6d3",
   "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>year</th>\n",
       "      <th>city</th>\n",
       "      <th>crime_major_category</th>\n",
       "      <th>crime_detail</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>occurance_time</th>\n",
       "      <th>clear_status</th>\n",
       "      <th>incident_address</th>\n",
       "      <th>notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>101855</th>\n",
       "      <td>2023</td>\n",
       "      <td>Chapel Hill</td>\n",
       "      <td>Disorderly Conduct</td>\n",
       "      <td>Assist Carrboro</td>\n",
       "      <td>35.90967</td>\n",
       "      <td>-79.06506</td>\n",
       "      <td>2023/06/24 20:43:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>W FRANKLIN ST   N MERRITT MILL RD</td>\n",
       "      <td>Weapon: None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101856</th>\n",
       "      <td>2023</td>\n",
       "      <td>Chapel Hill</td>\n",
       "      <td>Theft</td>\n",
       "      <td>Larceny From Motor Vehicle</td>\n",
       "      <td>35.92758</td>\n",
       "      <td>-79.03022</td>\n",
       "      <td>2023/09/12 17:45:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>WILLOW DR   S ESTES DR</td>\n",
       "      <td>Weapon: None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101857</th>\n",
       "      <td>2023</td>\n",
       "      <td>Chapel Hill</td>\n",
       "      <td>Traffic Violations</td>\n",
       "      <td>Information</td>\n",
       "      <td>35.96392</td>\n",
       "      <td>-79.06455</td>\n",
       "      <td>2023/10/03 15:59:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>WEAVER DAIRY RD EXT PALAFOX DR</td>\n",
       "      <td>Weapon: None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101858</th>\n",
       "      <td>2023</td>\n",
       "      <td>Chapel Hill</td>\n",
       "      <td>Assault</td>\n",
       "      <td>Disturbing The Peace</td>\n",
       "      <td>35.91316</td>\n",
       "      <td>-79.05578</td>\n",
       "      <td>2023/10/27 05:40:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>W FRANKLIN ST N COLUMBIA ST</td>\n",
       "      <td>Weapon: Knife/Cutting Instrument</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101859</th>\n",
       "      <td>2023</td>\n",
       "      <td>Chapel Hill</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Lost Property</td>\n",
       "      <td>35.91137</td>\n",
       "      <td>-79.06041</td>\n",
       "      <td>2023/05/20 22:30:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>W FRANKLIN STY</td>\n",
       "      <td>Weapon: None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        year         city crime_major_category                crime_detail  \\\n",
       "101855  2023  Chapel Hill   Disorderly Conduct             Assist Carrboro   \n",
       "101856  2023  Chapel Hill                Theft  Larceny From Motor Vehicle   \n",
       "101857  2023  Chapel Hill   Traffic Violations                 Information   \n",
       "101858  2023  Chapel Hill              Assault        Disturbing The Peace   \n",
       "101859  2023  Chapel Hill        Miscellaneous               Lost Property   \n",
       "\n",
       "        latitude  longitude       occurance_time clear_status  \\\n",
       "101855  35.90967  -79.06506  2023/06/24 20:43:00      No Data   \n",
       "101856  35.92758  -79.03022  2023/09/12 17:45:00      No Data   \n",
       "101857  35.96392  -79.06455  2023/10/03 15:59:00      No Data   \n",
       "101858  35.91316  -79.05578  2023/10/27 05:40:00      No Data   \n",
       "101859  35.91137  -79.06041  2023/05/20 22:30:00      No Data   \n",
       "\n",
       "                         incident_address                             notes  \n",
       "101855  W FRANKLIN ST   N MERRITT MILL RD                      Weapon: None  \n",
       "101856             WILLOW DR   S ESTES DR                      Weapon: None  \n",
       "101857     WEAVER DAIRY RD EXT PALAFOX DR                      Weapon: None  \n",
       "101858        W FRANKLIN ST N COLUMBIA ST  Weapon: Knife/Cutting Instrument  \n",
       "101859                     W FRANKLIN STY                      Weapon: None  "
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a mapping for crime categories\n",
    "category_mapping = {\n",
    "    'Theft': ['THEFT/LARCENY', 'LARCENY FROM AU', 'LARCENY FROM PE', 'LARCENY OF OTHE', 'LARCENY FROM BU', 'LARCENY OF BIKE', 'LARCENY FROM RE', 'LARCENY OF AUTO'],\n",
    "    'Assault': ['ASSAULT/SEXUAL', 'ASSAULT', 'STAB GUNSHOT PE', 'ACTIVE ASSAILAN'],\n",
    "    'Burglary': ['BURGLARY', 'BURGLARY ATTEMP', 'STRUCTURE COLLAPSE', 'ROBBERY/CARJACK'],\n",
    "    'Drugs': ['DRUGS'],\n",
    "    'Traffic Violations': ['TRAFFIC STOP', 'TRAFFIC/TRANSPO', 'TRAFFIC VIOLATI', 'MVC', 'MVC W INJURY', 'MVC W INJURY AB', 'MVC W INJURY DE', 'MVC ENTRAPMENT'],\n",
    "    'Disorderly Conduct': ['DISTURBANCE/NUI', 'DOMESTIC DISTUR', 'DISPUTE', 'DISTURBANCE', 'LOST PROPERTY', 'TRESPASSING/UNW', 'REFUSAL TO LEAV', 'SUSPICIOUS COND', 'STRUCTURE FIRE'],\n",
    "    'Fraud': ['FRAUD OR DECEPT'],\n",
    "    'Sexual Offenses': ['SEXUAL OFFENSE'],\n",
    "    'Homicide': ['SUICIDE ATTEMPT', 'ABUSE/ABANDOMEN', 'DECEASED PERSON'],\n",
    "    'Weapons Violations': ['WEAPON/FIREARMS'],\n",
    "    'Animal-related Offenses': ['ANIMAL BITE', 'ANIMAL', 'ANIMAL CALL'],\n",
    "    'Missing Person': ['MISSING PERSON'],\n",
    "    'Public Service': ['PUBLIC SERVICE', 'PUBLICE SERVICE'],\n",
    "    'Miscellaneous': ['<Null>', 'SUSPICIOUS/WANT', 'MISC OFFICER IN', 'INDECENCY/LEWDN', 'PUBLIC SERVICE', 'TRESPASSING', 'UNKNOWN PROBLEM', 'LOUD NOISE', 'ESCORT', 'ABDUCTION/CUSTO', 'THREATS', 'BURGLAR ALARM', 'DOMESTIC', 'PROPERTY FOUND', 'FIREWORKS', 'MISSING/RUNAWAY', 'MENTAL DISORDER', 'CHECK WELL BEIN', 'PSYCHIATRIC', 'OPEN DOOR', 'ABANDONED AUTO', 'HARASSMENT THRE', 'JUVENILE RELATE', 'ASSIST MOTORIST', 'HAZARDOUS DRIVI', 'MVC', 'GAS LEAK FIRE', 'ASSIST OTHER AG', 'DOMESTIC ASSIST', 'SUSPICIOUS VEHI', 'UNKNOWN LE', 'ALARMS', '911 HANGUP', 'BOMB/CBRN/PRODU', 'STATIONARY PATR', 'LITTERING', 'HOUSE CHECK', 'CARDIAC', 'CLOSE PATROL', 'BOMB FOUND/SUSP', 'INFO FOR ALL UN', 'UNCONCIOUS OR F', 'LIFTING ASSISTA', 'ATTEMPT TO LOCA', 'SICK PERSON', 'HEAT OR COLD EX', 'CONFINED SPACE', 'TRAUMATIC INJUR', 'DROWNING', 'CITY ORDINANCE']\n",
    "}\n",
    "\n",
    "\n",
    "# Function to categorize crime based on the mapping dictionary\n",
    "def categorize_crime(crime):\n",
    "    for category, crimes in category_mapping.items():\n",
    "        if crime in crimes:\n",
    "            return category\n",
    "    return 'Miscellaneous'\n",
    "\n",
    "# Create a new DataFrame with simplified crime categories\n",
    "Chapel_new = pd.DataFrame({\n",
    "    \"year\": pd.to_datetime(Chapel['Date_of_Occurrence']).dt.year,\n",
    "    \"city\": \"Chapel Hill\",\n",
    "    \"crime_major_category\": Chapel['Reported_As'].apply(categorize_crime),\n",
    "    \"crime_detail\": Chapel['Offense'].str.title(),\n",
    "    \"latitude\": Chapel['X'].round(5).fillna(0),\n",
    "    \"longitude\": Chapel['Y'].round(5).fillna(0),\n",
    "    \"occurance_time\": pd.to_datetime(Chapel['Date_of_Occurrence'].str.replace(r'\\+\\d{2}$', '', regex=True)).dt.strftime('%Y/%m/%d %H:%M:%S'),\n",
    "    \"clear_status\": None,\n",
    "    \"incident_address\": Chapel['Street'].str.replace(\"@\", \" \"),\n",
    "    \"notes\": Chapel['Weapon_Description'].apply(lambda x: f\"Weapon: {x}\" if pd.notnull(x) else \"Weapon: None\").str.title()\n",
    "}).fillna(\"No Data\")\n",
    "\n",
    "Chapel_new = Chapel_new[Chapel_new['year'] >= 2015]\n",
    "Chapel_new.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "dbbee2b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(67010, 10)"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Chapel_new.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab448155",
   "metadata": {},
   "source": [
    "# Cary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "ec11f03c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "Cary = pd.read_csv(\"Cary.csv\", low_memory = False).dropna(subset=['Year'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afa8a880",
   "metadata": {},
   "source": [
    "## Exploratory Data Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "5c5249e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Crime Category', 'Crime Type', 'UCR', 'Map Reference',\n",
       "       'Incident Number', 'Begin Date Of Occurrence',\n",
       "       'Begin Time Of Occurrence', 'End Date Of Occurrence',\n",
       "       'End Time Of Occurrence', 'Crime Day', 'Geo Code', 'Location Category',\n",
       "       'District', 'Beat Number', 'Location', 'ID', 'Lat', 'Lon',\n",
       "       'Charge Count', 'Neighborhood ID', 'Apartment Complex',\n",
       "       'Residential Subdivision', 'Subdivision ID', 'Phx Activity Date',\n",
       "       'Phx Record Status', 'Phx Community', 'Phx Status', 'Record',\n",
       "       'Offense Category', 'Violent Property', 'timeframe', 'domestic',\n",
       "       'Total Incidents', 'Year'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Cary.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "187a83c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['ALL OTHER', 'ARSON', 'AGGRAVATED ASSAULT', 'BURGLARY',\n",
       "       'MOTOR VEHICLE THEFT', 'MURDER', 'ROBBERY', 'LARCENY'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Cary['Crime Category'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "9f7ade9d",
   "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>year</th>\n",
       "      <th>city</th>\n",
       "      <th>crime_major_category</th>\n",
       "      <th>crime_detail</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>occurance_time</th>\n",
       "      <th>clear_status</th>\n",
       "      <th>incident_address</th>\n",
       "      <th>notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>116510</th>\n",
       "      <td>2022</td>\n",
       "      <td>Cary</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Missing Person</td>\n",
       "      <td>35.79749</td>\n",
       "      <td>-78.81358</td>\n",
       "      <td>2022/11/13 13:01:36</td>\n",
       "      <td>No Data</td>\n",
       "      <td>EDENHURST AVE</td>\n",
       "      <td>District: CpdnViolent Property: Non-Repor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116546</th>\n",
       "      <td>2022</td>\n",
       "      <td>Cary</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Missing Person</td>\n",
       "      <td>35.78252</td>\n",
       "      <td>-78.81503</td>\n",
       "      <td>2022/10/15 15:00:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>BYRAMS FORD DR</td>\n",
       "      <td>District: CpdsViolent Property: Non-Repor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116552</th>\n",
       "      <td>2022</td>\n",
       "      <td>Cary</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Missing Person</td>\n",
       "      <td>35.76020</td>\n",
       "      <td>-78.74427</td>\n",
       "      <td>2022/11/24 12:00:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>WALNUT ST</td>\n",
       "      <td>District: CpdsViolent Property: Non-Repor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116558</th>\n",
       "      <td>2018</td>\n",
       "      <td>Cary</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Missing Person</td>\n",
       "      <td>35.82342</td>\n",
       "      <td>-78.90523</td>\n",
       "      <td>2018/05/10 05:00:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>EMERALD DOWNS RD</td>\n",
       "      <td>District: D2Violent Property: Non-Repor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116573</th>\n",
       "      <td>2023</td>\n",
       "      <td>Cary</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Missing Person</td>\n",
       "      <td>35.78005</td>\n",
       "      <td>-78.75733</td>\n",
       "      <td>2023/07/22 23:30:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>FENTON GATEWAY DR</td>\n",
       "      <td>District: CpdsViolent Property: Non-Repor</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        year  city crime_major_category    crime_detail  latitude  longitude  \\\n",
       "116510  2022  Cary        Miscellaneous  Missing Person  35.79749  -78.81358   \n",
       "116546  2022  Cary        Miscellaneous  Missing Person  35.78252  -78.81503   \n",
       "116552  2022  Cary        Miscellaneous  Missing Person  35.76020  -78.74427   \n",
       "116558  2018  Cary        Miscellaneous  Missing Person  35.82342  -78.90523   \n",
       "116573  2023  Cary        Miscellaneous  Missing Person  35.78005  -78.75733   \n",
       "\n",
       "             occurance_time clear_status   incident_address  \\\n",
       "116510  2022/11/13 13:01:36      No Data      EDENHURST AVE   \n",
       "116546  2022/10/15 15:00:00      No Data     BYRAMS FORD DR   \n",
       "116552  2022/11/24 12:00:00      No Data          WALNUT ST   \n",
       "116558  2018/05/10 05:00:00      No Data   EMERALD DOWNS RD   \n",
       "116573  2023/07/22 23:30:00      No Data  FENTON GATEWAY DR   \n",
       "\n",
       "                                            notes  \n",
       "116510  District: CpdnViolent Property: Non-Repor  \n",
       "116546  District: CpdsViolent Property: Non-Repor  \n",
       "116552  District: CpdsViolent Property: Non-Repor  \n",
       "116558    District: D2Violent Property: Non-Repor  \n",
       "116573  District: CpdsViolent Property: Non-Repor  "
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = Cary\n",
    "def categorize_crime(crime):\n",
    "    crime_mapping = {\n",
    "        'Theft': ['BURGLARY', 'MOTOR VEHICLE THEFT', 'LARCENY'],\n",
    "        'Arson': ['ARSON'],\n",
    "        'Assault': ['AGGRAVATED ASSAULT'],\n",
    "        'Homicide': ['MURDER'],\n",
    "        'Robbery': ['ROBBERY']\n",
    "    }\n",
    "\n",
    "    for category, crimes in crime_mapping.items():\n",
    "        if crime in crimes:\n",
    "            return category\n",
    "    return 'Miscellaneous'\n",
    "\n",
    "Cary_new = pd.DataFrame({\n",
    "    \"year\": df[\"Year\"].astype(int),\n",
    "    \"city\": \"Cary\",\n",
    "    \"crime_major_category\": df['Crime Category'].apply(categorize_crime).str.title(),\n",
    "    \"crime_detail\": df['Crime Type'].str.title(),\n",
    "    \"latitude\": df['Lat'].fillna(0).round(5).fillna(0),\n",
    "    \"longitude\": df['Lon'].fillna(0).round(5).fillna(0),\n",
    "    \"occurance_time\": pd.to_datetime(df['Begin Date Of Occurrence'] + ' ' + df['Begin Time Of Occurrence']).dt.strftime('%Y/%m/%d %H:%M:%S'),\n",
    "    \"clear_status\": None,\n",
    "    \"incident_address\": df['Geo Code'],\n",
    "    \"notes\": 'District: '+ df['District'].str.title() + 'Violent Property: ' + df['Violent Property'].str.title()\n",
    "}).fillna(\"No Data\")\n",
    "\n",
    "Cary_new = Cary_new[Cary_new['year'] >= 2015]\n",
    "Cary_new.tail()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "05e9fedd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(44413, 10)"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Cary_new.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d3ed28a",
   "metadata": {},
   "source": [
    "# Raleigh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "22b3f101",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "Raleigh = pd.read_csv(\"Raleigh.csv\", low_memory=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "840ca1f4",
   "metadata": {},
   "source": [
    "## Exploratory Data Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "6984bfa7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['X', 'Y', 'OBJECTID', 'GlobalID', 'case_number', 'crime_category',\n",
       "       'crime_code', 'crime_description', 'crime_type',\n",
       "       'reported_block_address', 'city_of_incident', 'city', 'district',\n",
       "       'reported_date', 'reported_year', 'reported_month', 'reported_day',\n",
       "       'reported_hour', 'reported_dayofwk', 'latitude', 'longitude', 'agency',\n",
       "       'updated_date'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Raleigh.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "10da0e5f",
   "metadata": {
    "scrolled": true
   },
   "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>year</th>\n",
       "      <th>city</th>\n",
       "      <th>crime_major_category</th>\n",
       "      <th>crime_detail</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>occurance_time</th>\n",
       "      <th>clear_status</th>\n",
       "      <th>incident_address</th>\n",
       "      <th>notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>496313</th>\n",
       "      <td>2024</td>\n",
       "      <td>Raleigh</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Pornography/Obscene Material</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024/01/08 12:18:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>No Data</td>\n",
       "      <td>District: Northeast</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496314</th>\n",
       "      <td>2024</td>\n",
       "      <td>Raleigh</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Pornography/Obscene Material</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024/01/08 12:18:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>No Data</td>\n",
       "      <td>District: North</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496315</th>\n",
       "      <td>2024</td>\n",
       "      <td>Raleigh</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Pornography/Obscene Material</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024/01/08 12:19:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>No Data</td>\n",
       "      <td>District: Southeast</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496316</th>\n",
       "      <td>2024</td>\n",
       "      <td>Raleigh</td>\n",
       "      <td>Sexual Offenses</td>\n",
       "      <td>Sex Offense/Forcible Fondling</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024/02/14 21:52:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>No Data</td>\n",
       "      <td>District: North</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496317</th>\n",
       "      <td>2024</td>\n",
       "      <td>Raleigh</td>\n",
       "      <td>Sexual Offenses</td>\n",
       "      <td>Sex Offense/Statutory Rape</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2024/02/14 18:19:00</td>\n",
       "      <td>No Data</td>\n",
       "      <td>No Data</td>\n",
       "      <td>District: Northwest</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        year     city crime_major_category                   crime_detail  \\\n",
       "496313  2024  Raleigh        Miscellaneous   Pornography/Obscene Material   \n",
       "496314  2024  Raleigh        Miscellaneous   Pornography/Obscene Material   \n",
       "496315  2024  Raleigh        Miscellaneous   Pornography/Obscene Material   \n",
       "496316  2024  Raleigh      Sexual Offenses  Sex Offense/Forcible Fondling   \n",
       "496317  2024  Raleigh      Sexual Offenses     Sex Offense/Statutory Rape   \n",
       "\n",
       "        latitude  longitude       occurance_time clear_status  \\\n",
       "496313       0.0        0.0  2024/01/08 12:18:00      No Data   \n",
       "496314       0.0        0.0  2024/01/08 12:18:00      No Data   \n",
       "496315       0.0        0.0  2024/01/08 12:19:00      No Data   \n",
       "496316       0.0        0.0  2024/02/14 21:52:00      No Data   \n",
       "496317       0.0        0.0  2024/02/14 18:19:00      No Data   \n",
       "\n",
       "       incident_address                notes  \n",
       "496313          No Data  District: Northeast  \n",
       "496314          No Data      District: North  \n",
       "496315          No Data  District: Southeast  \n",
       "496316          No Data      District: North  \n",
       "496317          No Data  District: Northwest  "
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Define category mapping\n",
    "category_mapping = {\n",
    "    'Miscellaneous': ['MISCELLANEOUS', 'ALL OTHER OFFENSES'],\n",
    "    'Sexual Offenses': ['SEX OFFENSES'],\n",
    "    'Assault': ['ASSAULT', 'SIMPLE ASSAULT'],\n",
    "    'Juvenile': ['JUVENILE'],\n",
    "    'Traffic Violations': ['TRAFFIC', 'UNAUTHORIZED MOTOR VEHICLE USE', 'TRAFFIC VIOLATIONS', 'LIQUOR LAW VIOLATIONS'],\n",
    "    'Fraud': ['FRAUD', 'EMBEZZLEMENT', 'BRIBERY'],\n",
    "    'Vandalism': ['VANDALISM'],\n",
    "    'Theft': ['LARCENY FROM MV', 'LARCENY', 'MV THEFT', 'STOLEN PROPERTY'],\n",
    "    'Burglary': ['BURGLARY/COMMERCIAL', 'BURGLARY/RESIDENTIAL'],\n",
    "    'Disorderly Conduct': ['DISORDERLY CONDUCT'],\n",
    "    'Weapons Violations': ['WEAPONS VIOLATION'],\n",
    "    'Drugs': ['DRUGS', 'DRUG VIOLATIONS'],\n",
    "    'Arson': ['ARSON'],\n",
    "    'Robbery': ['ROBBERY'],\n",
    "    'Kidnapping': ['KIDNAPPING'],\n",
    "    'Extortion': ['EXTORTION'],\n",
    "    'Human Trafficking': ['HUMAN TRAFFICKING'],\n",
    "    'Murder': ['MURDER'],\n",
    "    'Prostitution-related Offenses': ['PROSTITUTION'],\n",
    "    'Gambling': ['GAMBLING'],\n",
    "}\n",
    "\n",
    "# Function to categorize crime based on the mapping dictionary\n",
    "def categorize_crime(crime):\n",
    "    for category, crimes in category_mapping.items():\n",
    "        if crime in crimes:\n",
    "            return category\n",
    "    return 'Miscellaneous'\n",
    "\n",
    "# Create a new DataFrame with simplified crime categories\n",
    "Raleigh_new = pd.DataFrame({\n",
    "    \"year\": Raleigh['reported_year'],\n",
    "    \"city\": \"Raleigh\",\n",
    "    \"crime_major_category\": Raleigh['crime_category'].apply(categorize_crime),\n",
    "    \"crime_detail\": Raleigh['crime_description'],\n",
    "    \"latitude\": Raleigh['latitude'].round(5).fillna(0),\n",
    "    \"longitude\": Raleigh['longitude'].round(5).fillna(0),\n",
    "    \"occurance_time\": pd.to_datetime(Raleigh['reported_date'].str.replace(r'\\+\\d{2}$', '', regex=True), errors='coerce').dt.strftime('%Y/%m/%d %H:%M:%S'),\n",
    "    \"clear_status\": None,\n",
    "    \"incident_address\": Raleigh['reported_block_address'] + ', ' + Raleigh['district'] + ', Raleigh',\n",
    "    \"notes\": 'District: '+ Raleigh['district'].str.title()\n",
    "}).fillna(\"No Data\")\n",
    "\n",
    "Raleigh_new = Raleigh_new[Raleigh_new['year'] >= 2015]\n",
    "# Display the last few rows of the new DataFrame\n",
    "Raleigh_new.tail()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "4561f484",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(466007, 10)"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Raleigh_new.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "b36410ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35.99118"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(Raleigh_new['latitude'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "a4faca3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2017, 2016, 2015, 2018, 2019, 2020, 2021, 2022, 2023, 2024])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Raleigh_new['year'].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0ad795d",
   "metadata": {},
   "source": [
    "# Combined"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "82ed2351",
   "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>year</th>\n",
       "      <th>city</th>\n",
       "      <th>crime_major_category</th>\n",
       "      <th>crime_detail</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>occurance_time</th>\n",
       "      <th>clear_status</th>\n",
       "      <th>incident_address</th>\n",
       "      <th>notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Miscellaneous</td>\n",
       "      <td>Intimidation</td>\n",
       "      <td>36.03734</td>\n",
       "      <td>-78.87843</td>\n",
       "      <td>2022/05/20 12:00:00</td>\n",
       "      <td>Cleared By Arrest</td>\n",
       "      <td>3500 DEARBORN DR</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Fraud</td>\n",
       "      <td>Fraud-Identity Theft</td>\n",
       "      <td>35.90624</td>\n",
       "      <td>-78.90556</td>\n",
       "      <td>2019/01/01 00:01:00</td>\n",
       "      <td>Inactive</td>\n",
       "      <td>4400 EMERALD FOREST DR</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Theft</td>\n",
       "      <td>Larceny - Automobile Parts Or Accessories</td>\n",
       "      <td>35.98809</td>\n",
       "      <td>-78.88952</td>\n",
       "      <td>2020/03/09 00:00:00</td>\n",
       "      <td>Active/Open</td>\n",
       "      <td>100 EDGEMONT LN</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Theft</td>\n",
       "      <td>Towed/Abandoned Vehicle</td>\n",
       "      <td>35.92415</td>\n",
       "      <td>-78.78532</td>\n",
       "      <td>2022/07/19 10:30:00</td>\n",
       "      <td>Closed (Non-Criminal)</td>\n",
       "      <td>1000 ANDREWS CHAPEL RD</td>\n",
       "      <td>Weapon: (blank)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018</td>\n",
       "      <td>Durham</td>\n",
       "      <td>Drugs</td>\n",
       "      <td>Drug/Narcotic Violations</td>\n",
       "      <td>35.97721</td>\n",
       "      <td>-78.89507</td>\n",
       "      <td>2018/10/18 14:35:00</td>\n",
       "      <td>Inactive</td>\n",
       "      <td>800 DUPREE ST</td>\n",
       "      <td>Weapon: Not Applicable/None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year    city crime_major_category  \\\n",
       "0  2022  Durham        Miscellaneous   \n",
       "1  2019  Durham                Fraud   \n",
       "2  2020  Durham                Theft   \n",
       "3  2022  Durham                Theft   \n",
       "4  2018  Durham                Drugs   \n",
       "\n",
       "                                crime_detail  latitude  longitude  \\\n",
       "0                               Intimidation  36.03734  -78.87843   \n",
       "1                       Fraud-Identity Theft  35.90624  -78.90556   \n",
       "2  Larceny - Automobile Parts Or Accessories  35.98809  -78.88952   \n",
       "3                    Towed/Abandoned Vehicle  35.92415  -78.78532   \n",
       "4                   Drug/Narcotic Violations  35.97721  -78.89507   \n",
       "\n",
       "        occurance_time           clear_status        incident_address  \\\n",
       "0  2022/05/20 12:00:00      Cleared By Arrest        3500 DEARBORN DR   \n",
       "1  2019/01/01 00:01:00               Inactive  4400 EMERALD FOREST DR   \n",
       "2  2020/03/09 00:00:00            Active/Open         100 EDGEMONT LN   \n",
       "3  2022/07/19 10:30:00  Closed (Non-Criminal)  1000 ANDREWS CHAPEL RD   \n",
       "4  2018/10/18 14:35:00               Inactive           800 DUPREE ST   \n",
       "\n",
       "                         notes  \n",
       "0              Weapon: (blank)  \n",
       "1              Weapon: (blank)  \n",
       "2              Weapon: (blank)  \n",
       "3              Weapon: (blank)  \n",
       "4  Weapon: Not Applicable/None  "
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "NC = pd.concat([Durham_new, Chapel_new, Cary_new, Raleigh_new], ignore_index=True)\n",
    "NC.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "ebd44953",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(727352, 10)"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "NC.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "51b553ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 727352 entries, 0 to 727351\n",
      "Data columns (total 10 columns):\n",
      " #   Column                Non-Null Count   Dtype  \n",
      "---  ------                --------------   -----  \n",
      " 0   year                  727352 non-null  int64  \n",
      " 1   city                  727352 non-null  object \n",
      " 2   crime_major_category  727352 non-null  object \n",
      " 3   crime_detail          727352 non-null  object \n",
      " 4   latitude              727352 non-null  float64\n",
      " 5   longitude             727352 non-null  float64\n",
      " 6   occurance_time        727352 non-null  object \n",
      " 7   clear_status          727352 non-null  object \n",
      " 8   incident_address      727352 non-null  object \n",
      " 9   notes                 727352 non-null  object \n",
      "dtypes: float64(2), int64(1), object(7)\n",
      "memory usage: 55.5+ MB\n"
     ]
    }
   ],
   "source": [
    "NC.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "5c45ae42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Miscellaneous', 'Fraud', 'Theft', 'Drugs', 'Assault',\n",
       "       'Sexual Offenses', 'Traffic Violations', 'Kidnapping',\n",
       "       'Weapons Violations', 'Homicide', 'Disorderly Conduct', 'Arson',\n",
       "       'Animal-related Offenses', 'Prostitution-related Offenses',\n",
       "       'Gambling', 'Public Service', 'Burglary', 'Missing Person',\n",
       "       'Robbery', 'Juvenile', 'Vandalism', 'Extortion',\n",
       "       'Human Trafficking', 'Murder'], dtype=object)"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "NC['crime_major_category'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "7fdffffb",
   "metadata": {},
   "outputs": [],
   "source": [
    "NC.to_csv('NC_dataset.csv', index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "489faa5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year                      int64\n",
      "city                     object\n",
      "crime_major_category     object\n",
      "crime_detail             object\n",
      "latitude                float64\n",
      "longitude               float64\n",
      "occurance_time           object\n",
      "clear_status             object\n",
      "incident_address         object\n",
      "notes                    object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(NC.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "b4885d64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024])"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.sort(NC['year'].unique())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}