zwn22 commited on
Commit
a2c06da
1 Parent(s): 8c785f1

Upload EDA.ipynb

Browse files
Files changed (1) hide show
  1. EDA.ipynb +2071 -0
EDA.ipynb ADDED
@@ -0,0 +1,2071 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "f50e09b6",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Introduction"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "markdown",
13
+ "id": "ff929199",
14
+ "metadata": {},
15
+ "source": [
16
+ "This is the EDA script for processing the huggingface dataset \"zwn22/NC_Crime\"."
17
+ ]
18
+ },
19
+ {
20
+ "cell_type": "markdown",
21
+ "id": "356beb0a",
22
+ "metadata": {},
23
+ "source": [
24
+ "# Durham "
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "markdown",
29
+ "id": "f31470ef",
30
+ "metadata": {},
31
+ "source": [
32
+ "NC State Plane ESPG: 2264 https://epsg.io/2264"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 86,
38
+ "id": "ae2c8d75",
39
+ "metadata": {},
40
+ "outputs": [],
41
+ "source": [
42
+ "import pandas as pd\n",
43
+ "Durham = pd.read_excel('Durham.xlsx')"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "markdown",
48
+ "id": "88a16949",
49
+ "metadata": {},
50
+ "source": [
51
+ "## Exploratory Data Analysis"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": 87,
57
+ "id": "857af355",
58
+ "metadata": {},
59
+ "outputs": [
60
+ {
61
+ "data": {
62
+ "text/plain": [
63
+ "Index(['Case Number', 'Report Date', 'Report Time', 'Status', 'Sequence',\n",
64
+ " 'ATT/COM', 'UCR Code', 'Offense', 'Address', 'X', 'Y', 'District',\n",
65
+ " 'Beat', 'Tract', 'Premise', 'Weapon'],\n",
66
+ " dtype='object')"
67
+ ]
68
+ },
69
+ "execution_count": 87,
70
+ "metadata": {},
71
+ "output_type": "execute_result"
72
+ }
73
+ ],
74
+ "source": [
75
+ "Durham.columns"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": 2,
81
+ "id": "6059809c",
82
+ "metadata": {},
83
+ "outputs": [
84
+ {
85
+ "data": {
86
+ "text/plain": [
87
+ "array(['(blank)', 'Not Applicable/None', 'Unknown/Not Stated',\n",
88
+ " 'Personal Weapons', 'Knife/Cutting Instrument', 'Handgun',\n",
89
+ " 'Blunt Objects', 'Rifle', 'Asphyxiation', 'Other Weapon',\n",
90
+ " 'Narcotics/Drugs', 'Undetermined Firearm', 'Shotgun',\n",
91
+ " 'Motor Vehicle/Vessel', 'Fire/Burning Tool/Device',\n",
92
+ " 'Other Firearm', 'Explosives', 'Poison'], dtype=object)"
93
+ ]
94
+ },
95
+ "execution_count": 2,
96
+ "metadata": {},
97
+ "output_type": "execute_result"
98
+ }
99
+ ],
100
+ "source": [
101
+ "Durham['Weapon'].unique()"
102
+ ]
103
+ },
104
+ {
105
+ "cell_type": "code",
106
+ "execution_count": 3,
107
+ "id": "e836498c",
108
+ "metadata": {},
109
+ "outputs": [],
110
+ "source": [
111
+ "Durham['Weapon'] = Durham['Weapon'].replace(['(blank)', 'Not Applicable/None', 'Unknown/Not Stated'], None)"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": 4,
117
+ "id": "a484806d",
118
+ "metadata": {},
119
+ "outputs": [
120
+ {
121
+ "data": {
122
+ "text/plain": [
123
+ "array(['INTIMIDATION', 'FRAUD-IDENTITY THEFT',\n",
124
+ " 'LARCENY - AUTOMOBILE PARTS OR ACCESSORIES',\n",
125
+ " 'TOWED/ABANDONED VEHICLE', 'DRUG/NARCOTIC VIOLATIONS',\n",
126
+ " 'DRUG EQUIPMENT/PARAPHERNALIA', 'MOTOR VEHICLE THEFT', 'BURGLARY',\n",
127
+ " 'SIMPLE ASSAULT', 'LARCENY - FROM MOTOR VEHICLE',\n",
128
+ " 'LARCENY - SHOPLIFTING', 'LOST PROPERTY', 'VANDALISM',\n",
129
+ " 'LARCENY - ALL OTHER', 'DV INFO REPORT (NO CRIME)',\n",
130
+ " 'ALL OTHER CRIMINAL OFFENSES', 'LARCENY - FROM BUILDING',\n",
131
+ " 'ALL OTHER OFFENSES-PAROLE/PROBATION VIOLATIONS', 'EMBEZZLEMENT',\n",
132
+ " 'ASSIST OTHER AGENCY', 'AGGRAVATED ASSAULT',\n",
133
+ " 'SEX OFFENSE - FORCIBLE RAPE',\n",
134
+ " 'SEX OFFENSE - SEXUAL ASSAULT WITH AN OBJECT',\n",
135
+ " 'COUNTERFEITING/FORGERY', 'FRAUD - CONFIDENCE GAMES/TRICKERY',\n",
136
+ " 'SUSPICIOUS ACTIVITY', 'CALLS FOR SERVICE (NO CRIME)',\n",
137
+ " 'ROBBERY - COMMERCIAL', 'ROBBERY - INDIVIDUAL',\n",
138
+ " 'FRAUD - CREDIT CARD/ATM',\n",
139
+ " 'FRAUD - UNAUTHORIZED USE OF CONVEYANCE', 'RUNAWAY',\n",
140
+ " 'RECOVERED STOLEN PROPERTY (OTHER JURISDICTION)',\n",
141
+ " 'ALL OTHER OFFENSES-COURT VIOLATIONS', 'DEATH INVESTIGATION',\n",
142
+ " 'FRAUD - FALSE PRETENSE', 'SUICIDE', 'ALL TRAFFIC (EXCEPT DWI)',\n",
143
+ " 'RECOVERED STOLEN VEHICLE (OTHER JURISDICTION)', 'TRESPASSING',\n",
144
+ " 'FRAUD - IMPERSONATION', 'KIDNAPPING/ABDUCTION',\n",
145
+ " 'WEAPON VIOLATIONS', 'ALL OTHER OFFENSES - HARASSING PHONE CALLS',\n",
146
+ " 'FOUND PROPERTY', 'FRAUD - WIRE/COMPUTER/OTHER ELECTRONIC',\n",
147
+ " 'FRAUD - WORTHLESS CHECKS', 'OFFENSE AGAINST FAMILY - OTHER',\n",
148
+ " 'NON-CRIMINAL DETAINMENT (INVOLUNTARY COMMITMENT)',\n",
149
+ " 'SEX OFFENSE - FONDLING', 'UNDISCIPLINED JUVENILE',\n",
150
+ " 'LIQUOR LAW VIOLATIONS', 'BLACKMAIL/EXTORTION',\n",
151
+ " 'DRIVING WHILE IMPAIRED', 'CRIME SCENE INVESTIGATION',\n",
152
+ " 'MISSING PERSON', 'STOLEN PROPERTY',\n",
153
+ " 'HOMICIDE-MURDER/NON-NEGLIGENT MANSLAUGHTER',\n",
154
+ " 'DISORDERLY CONDUCT-DRUNK AND DISRUPTIVE',\n",
155
+ " 'OFFENSES AGAINST FAMILY - CHILD ABUSE',\n",
156
+ " 'ALL OTHER OFFENSES-ESCAPE FROM CUSTODY OR RESIST ARREST',\n",
157
+ " 'CURFEW/LOITERING/VAGRANCY VIOLATIONS',\n",
158
+ " 'SEX OFFENSE - FORCIBLE SODOMY',\n",
159
+ " 'ALL OTHER OFFENSES-CITY ORDINANCE VIOLATIONS',\n",
160
+ " 'PORNOGRAPHY/OBSCENE MATERIAL', 'ARSON', 'DISORDERLY CONDUCT',\n",
161
+ " 'OFFENSES AGAINST FAMILY - NEGLECT',\n",
162
+ " 'SEX OFFENSE - INDECENT EXPOSURE',\n",
163
+ " 'DISORDERLY CONDUCT-FIGHTING (AFFRAY)', 'ROBBERY - BANK',\n",
164
+ " 'LARCENY - POCKET-PICKING', 'LARCENY - FROM COIN-OPERATED DEVICE',\n",
165
+ " 'FRAUD - WELFARE FRAUD', 'ANIMAL CRUELTY',\n",
166
+ " 'SEX OFFENSE - STATUTORY RAPE', 'LARCENY - PURSESNATCHING',\n",
167
+ " 'PROSTITUTION', 'FRAUD-FAIL TO RETURN RENTAL VEHICLE',\n",
168
+ " 'OFFENSES AGAINST FAMILY - DESERTION/ABANDONMENT', 'BRIBERY',\n",
169
+ " 'SEX OFFENSE - PEEPING TOM', 'JUSTIFIABLE HOMICIDE',\n",
170
+ " 'FRAUD-HACKING/COMPUTER INVASION', 'TRUANCY',\n",
171
+ " 'HUMAN TRAFFICKING/INVOLUNTARY SERVITUDE',\n",
172
+ " 'HOMICIDE - NEGLIGENT MANSLAUGHTER',\n",
173
+ " 'RESIST ARREST, ETC-REPEALED DO NOT USE',\n",
174
+ " 'VANDALISM TO AUTO (NOT ACCIDENTAL)',\n",
175
+ " 'DOMESTIC VIOLENCE ORDER VIOL-REPEALED DO NOT USE',\n",
176
+ " 'GAMBLING - OPERATING/PROMOTING/ASSISTING',\n",
177
+ " 'PROSTITUTION - ASSISTING/PROMOTING', 'SEX OFFENSE - INCEST',\n",
178
+ " 'GAMBLING - BETTING/WAGERING', 'PROSTITUTION - PURCHASING',\n",
179
+ " 'DISORDERLY CONDUCT-UNLAWFUL ASSEMBLY',\n",
180
+ " 'HUMAN TRAFFICKING/COMMERCIAL SEX ACTS'], dtype=object)"
181
+ ]
182
+ },
183
+ "execution_count": 4,
184
+ "metadata": {},
185
+ "output_type": "execute_result"
186
+ }
187
+ ],
188
+ "source": [
189
+ "Durham['Offense'].unique()"
190
+ ]
191
+ },
192
+ {
193
+ "cell_type": "code",
194
+ "execution_count": 88,
195
+ "id": "da37a48b",
196
+ "metadata": {},
197
+ "outputs": [],
198
+ "source": [
199
+ "from pyproj import Transformer\n",
200
+ "\n",
201
+ "def convert_coordinates(x, y):\n",
202
+ "\n",
203
+ " transformer = Transformer.from_crs(\"epsg:2264\", \"epsg:4326\", always_xy=True) # 注意设置always_xy=True以保持x,y顺序\n",
204
+ "\n",
205
+ " lon, lat = transformer.transform(x, y)\n",
206
+ " \n",
207
+ " return pd.Series([lat, lon])"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": 89,
213
+ "id": "eed49097",
214
+ "metadata": {},
215
+ "outputs": [],
216
+ "source": [
217
+ "# Create a mapping dictionary for crime categories\n",
218
+ "category_mapping = {\n",
219
+ " '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",
220
+ " '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",
221
+ " 'Assault': ['SIMPLE ASSAULT', 'AGGRAVATED ASSAULT'],\n",
222
+ " 'Drugs': ['DRUG/NARCOTIC VIOLATIONS', 'DRUG EQUIPMENT/PARAPHERNALIA'],\n",
223
+ " '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",
224
+ " 'Homicide': ['HOMICIDE-MURDER/NON-NEGLIGENT MANSLAUGHTER', 'JUSTIFIABLE HOMICIDE', 'HOMICIDE - NEGLIGENT MANSLAUGHTER'],\n",
225
+ " 'Arson': ['ARSON'],\n",
226
+ " 'Kidnapping': ['KIDNAPPING/ABDUCTION'],\n",
227
+ " 'Weapons Violations': ['WEAPON VIOLATIONS'],\n",
228
+ " 'Traffic Violations': ['ALL TRAFFIC (EXCEPT DWI)'],\n",
229
+ " 'Disorderly Conduct': ['DISORDERLY CONDUCT', 'DISORDERLY CONDUCT-DRUNK AND DISRUPTIVE', 'DISORDERLY CONDUCT-FIGHTING (AFFRAY)', 'DISORDERLY CONDUCT-UNLAWFUL ASSEMBLY'],\n",
230
+ " 'Gambling': ['GAMBLING - OPERATING/PROMOTING/ASSISTING', 'GAMBLING - BETTING/WAGERING'],\n",
231
+ " 'Animal-related Offenses': ['ANIMAL CRUELTY'],\n",
232
+ " 'Prostitution-related Offenses': ['PROSTITUTION', 'PROSTITUTION - ASSISTING/PROMOTING', 'PROSTITUTION - PURCHASING']\n",
233
+ "}\n",
234
+ "\n",
235
+ "# Function to categorize crime based on the mapping dictionary\n",
236
+ "def categorize_crime(crime):\n",
237
+ " for category, crimes in category_mapping.items():\n",
238
+ " if crime in crimes:\n",
239
+ " return category\n",
240
+ " return 'Miscellaneous'\n",
241
+ "\n",
242
+ "# Create a new DataFrame with simplified crime categories\n",
243
+ "Durham_new = pd.DataFrame({\n",
244
+ " \"year\": pd.to_datetime(Durham['Report Date']).dt.year,\n",
245
+ " \"city\": \"Durham\",\n",
246
+ " \"crime_major_category\": Durham['Offense'].apply(categorize_crime),\n",
247
+ " \"crime_detail\": Durham['Offense'].str.title(),\n",
248
+ " \"latitude\": Durham['X'],\n",
249
+ " \"longitude\": Durham['Y'],\n",
250
+ " \"occurance_time\": pd.to_datetime(Durham['Report Date'] + ' ' + Durham['Report Time']).dt.strftime('%Y/%m/%d %H:%M:%S'),\n",
251
+ " \"clear_status\": Durham['Status'],\n",
252
+ " \"incident_address\": Durham['Address'],\n",
253
+ " \"notes\": Durham['Weapon'].apply(lambda x: f\"Weapon: {x}\" if pd.notnull(x) else \"No Data\")\n",
254
+ "})\n",
255
+ "\n",
256
+ "Durham_new[['latitude', 'longitude']] = Durham.apply(lambda row: convert_coordinates(row['X'], row['Y']), axis=1).round(5).fillna(0)\n"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": 92,
262
+ "id": "c8d699cf",
263
+ "metadata": {},
264
+ "outputs": [],
265
+ "source": [
266
+ "Durham_new = Durham_new[Durham_new['year'] >= 2015].fillna(\"No Data\")"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": 93,
272
+ "id": "fe3be70e",
273
+ "metadata": {},
274
+ "outputs": [
275
+ {
276
+ "data": {
277
+ "text/html": [
278
+ "<div>\n",
279
+ "<style scoped>\n",
280
+ " .dataframe tbody tr th:only-of-type {\n",
281
+ " vertical-align: middle;\n",
282
+ " }\n",
283
+ "\n",
284
+ " .dataframe tbody tr th {\n",
285
+ " vertical-align: top;\n",
286
+ " }\n",
287
+ "\n",
288
+ " .dataframe thead th {\n",
289
+ " text-align: right;\n",
290
+ " }\n",
291
+ "</style>\n",
292
+ "<table border=\"1\" class=\"dataframe\">\n",
293
+ " <thead>\n",
294
+ " <tr style=\"text-align: right;\">\n",
295
+ " <th></th>\n",
296
+ " <th>year</th>\n",
297
+ " <th>city</th>\n",
298
+ " <th>crime_major_category</th>\n",
299
+ " <th>crime_detail</th>\n",
300
+ " <th>latitude</th>\n",
301
+ " <th>longitude</th>\n",
302
+ " <th>occurance_time</th>\n",
303
+ " <th>clear_status</th>\n",
304
+ " <th>incident_address</th>\n",
305
+ " <th>notes</th>\n",
306
+ " </tr>\n",
307
+ " </thead>\n",
308
+ " <tbody>\n",
309
+ " <tr>\n",
310
+ " <th>149919</th>\n",
311
+ " <td>2022</td>\n",
312
+ " <td>Durham</td>\n",
313
+ " <td>Theft</td>\n",
314
+ " <td>Larceny - From Motor Vehicle</td>\n",
315
+ " <td>35.88024</td>\n",
316
+ " <td>-78.85024</td>\n",
317
+ " <td>2022/04/13 00:15:00</td>\n",
318
+ " <td>Cleared By Arrest</td>\n",
319
+ " <td>5400 S MIAMI BLVD</td>\n",
320
+ " <td>Weapon: (blank)</td>\n",
321
+ " </tr>\n",
322
+ " <tr>\n",
323
+ " <th>149920</th>\n",
324
+ " <td>2022</td>\n",
325
+ " <td>Durham</td>\n",
326
+ " <td>Miscellaneous</td>\n",
327
+ " <td>Recovered Stolen Vehicle (Other Jurisdiction)</td>\n",
328
+ " <td>35.88037</td>\n",
329
+ " <td>-78.85057</td>\n",
330
+ " <td>2022/04/13 00:15:00</td>\n",
331
+ " <td>Cleared By Arrest</td>\n",
332
+ " <td>5400 S MIAMI BLVD</td>\n",
333
+ " <td>Weapon: (blank)</td>\n",
334
+ " </tr>\n",
335
+ " <tr>\n",
336
+ " <th>149921</th>\n",
337
+ " <td>2022</td>\n",
338
+ " <td>Durham</td>\n",
339
+ " <td>Assault</td>\n",
340
+ " <td>Aggravated Assault</td>\n",
341
+ " <td>35.96519</td>\n",
342
+ " <td>-78.94559</td>\n",
343
+ " <td>2022/12/10 01:55:00</td>\n",
344
+ " <td>Cleared By Exception</td>\n",
345
+ " <td>3200 OLD CHAPEL HILL RD</td>\n",
346
+ " <td>Weapon: Handgun</td>\n",
347
+ " </tr>\n",
348
+ " <tr>\n",
349
+ " <th>149922</th>\n",
350
+ " <td>2022</td>\n",
351
+ " <td>Durham</td>\n",
352
+ " <td>Theft</td>\n",
353
+ " <td>Vandalism</td>\n",
354
+ " <td>35.88496</td>\n",
355
+ " <td>-78.84567</td>\n",
356
+ " <td>2022/12/31 00:00:00</td>\n",
357
+ " <td>Active/Open</td>\n",
358
+ " <td>100 TATUM DR</td>\n",
359
+ " <td>Weapon: (blank)</td>\n",
360
+ " </tr>\n",
361
+ " <tr>\n",
362
+ " <th>149923</th>\n",
363
+ " <td>2022</td>\n",
364
+ " <td>Durham</td>\n",
365
+ " <td>Fraud</td>\n",
366
+ " <td>Fraud - Credit Card/Atm</td>\n",
367
+ " <td>35.99019</td>\n",
368
+ " <td>-78.89111</td>\n",
369
+ " <td>2022/05/03 08:36:00</td>\n",
370
+ " <td>Cleared By Arrest</td>\n",
371
+ " <td>800 E MAIN ST</td>\n",
372
+ " <td>Weapon: (blank)</td>\n",
373
+ " </tr>\n",
374
+ " </tbody>\n",
375
+ "</table>\n",
376
+ "</div>"
377
+ ],
378
+ "text/plain": [
379
+ " year city crime_major_category \\\n",
380
+ "149919 2022 Durham Theft \n",
381
+ "149920 2022 Durham Miscellaneous \n",
382
+ "149921 2022 Durham Assault \n",
383
+ "149922 2022 Durham Theft \n",
384
+ "149923 2022 Durham Fraud \n",
385
+ "\n",
386
+ " crime_detail latitude longitude \\\n",
387
+ "149919 Larceny - From Motor Vehicle 35.88024 -78.85024 \n",
388
+ "149920 Recovered Stolen Vehicle (Other Jurisdiction) 35.88037 -78.85057 \n",
389
+ "149921 Aggravated Assault 35.96519 -78.94559 \n",
390
+ "149922 Vandalism 35.88496 -78.84567 \n",
391
+ "149923 Fraud - Credit Card/Atm 35.99019 -78.89111 \n",
392
+ "\n",
393
+ " occurance_time clear_status incident_address \\\n",
394
+ "149919 2022/04/13 00:15:00 Cleared By Arrest 5400 S MIAMI BLVD \n",
395
+ "149920 2022/04/13 00:15:00 Cleared By Arrest 5400 S MIAMI BLVD \n",
396
+ "149921 2022/12/10 01:55:00 Cleared By Exception 3200 OLD CHAPEL HILL RD \n",
397
+ "149922 2022/12/31 00:00:00 Active/Open 100 TATUM DR \n",
398
+ "149923 2022/05/03 08:36:00 Cleared By Arrest 800 E MAIN ST \n",
399
+ "\n",
400
+ " notes \n",
401
+ "149919 Weapon: (blank) \n",
402
+ "149920 Weapon: (blank) \n",
403
+ "149921 Weapon: Handgun \n",
404
+ "149922 Weapon: (blank) \n",
405
+ "149923 Weapon: (blank) "
406
+ ]
407
+ },
408
+ "execution_count": 93,
409
+ "metadata": {},
410
+ "output_type": "execute_result"
411
+ }
412
+ ],
413
+ "source": [
414
+ "Durham_new.tail(5)"
415
+ ]
416
+ },
417
+ {
418
+ "cell_type": "code",
419
+ "execution_count": 94,
420
+ "id": "55fc0352",
421
+ "metadata": {},
422
+ "outputs": [
423
+ {
424
+ "data": {
425
+ "text/plain": [
426
+ "(149922, 10)"
427
+ ]
428
+ },
429
+ "execution_count": 94,
430
+ "metadata": {},
431
+ "output_type": "execute_result"
432
+ }
433
+ ],
434
+ "source": [
435
+ "Durham_new.shape"
436
+ ]
437
+ },
438
+ {
439
+ "cell_type": "markdown",
440
+ "id": "1a87d3c7",
441
+ "metadata": {},
442
+ "source": [
443
+ "# Chapel Hill"
444
+ ]
445
+ },
446
+ {
447
+ "cell_type": "code",
448
+ "execution_count": 90,
449
+ "id": "9f002d85",
450
+ "metadata": {},
451
+ "outputs": [],
452
+ "source": [
453
+ "import pandas as pd\n",
454
+ "Chapel = pd.read_csv(\"Chapel_hill.csv\", low_memory=False)"
455
+ ]
456
+ },
457
+ {
458
+ "cell_type": "markdown",
459
+ "id": "20afd6f7",
460
+ "metadata": {},
461
+ "source": [
462
+ "## Exploratory Data Analysis"
463
+ ]
464
+ },
465
+ {
466
+ "cell_type": "code",
467
+ "execution_count": 22,
468
+ "id": "00878dd6",
469
+ "metadata": {},
470
+ "outputs": [
471
+ {
472
+ "data": {
473
+ "text/plain": [
474
+ "Index(['X', 'Y', 'Incident_ID', 'Agency', 'Offense', 'Street', 'City', 'State',\n",
475
+ " 'Zipcode', 'Date_of_Report', 'Date_of_Occurrence', 'Date_Found',\n",
476
+ " 'Reported_As', 'Premise_Description', 'Forcible', 'Weapon_Description',\n",
477
+ " 'Victim_Age', 'Victim_Race', 'Victim_Gender', 'Latitude', 'Longitude',\n",
478
+ " 'ObjectId'],\n",
479
+ " dtype='object')"
480
+ ]
481
+ },
482
+ "execution_count": 22,
483
+ "metadata": {},
484
+ "output_type": "execute_result"
485
+ }
486
+ ],
487
+ "source": [
488
+ "Chapel.columns"
489
+ ]
490
+ },
491
+ {
492
+ "cell_type": "code",
493
+ "execution_count": 95,
494
+ "id": "30789db9",
495
+ "metadata": {},
496
+ "outputs": [
497
+ {
498
+ "data": {
499
+ "text/plain": [
500
+ "array(['<Null>', 'DISTURBANCE/NUI', 'SUSPICIOUS/WANT', 'TRAFFIC STOP',\n",
501
+ " 'THEFT/LARCENY', 'MISC OFFICER IN', 'BURGLARY/HOME I',\n",
502
+ " 'INDECENCY/LEWDN', 'TRAFFIC/TRANSPO', 'MVC W INJURY',\n",
503
+ " 'DAMAGE/VANDALIS', 'PUBLIC SERVICE', 'TRESPASSING/UNW',\n",
504
+ " 'INFO MESSAGE', 'HARASSMENT/STAL', 'DOMESTIC DISTUR',\n",
505
+ " 'ADMINISTRATIVE', 'ELECTRICAL HAZA', 'CARDIAC ARREST',\n",
506
+ " 'FRAUD OR DECEPT', 'ASSIST CITIZEN', 'ASSAULT/SEXUAL',\n",
507
+ " 'INTOXICATED SUB', 'LE ASSISTANCE', 'DRUGS', 'SUSPICIOUS OR W',\n",
508
+ " 'ARREST', 'DISPUTE', 'DISTURBANCE', 'BURGLARY', 'ASSIST OTHR AGE',\n",
509
+ " 'LARCENY FROM AU', 'TRESPASSING', 'DAMAGE TO PROPE',\n",
510
+ " 'REFUSAL TO LEAV', 'UNKNOWN PROBLEM', 'WEAPON/FIREARMS',\n",
511
+ " 'LOUD NOISE', 'ESCORT', 'ABDUCTION/CUSTO', 'VANDALISM',\n",
512
+ " 'LARCENY OF OTHE', 'LARCENY FROM PE', 'THREATS', 'LARCENY FROM BU',\n",
513
+ " 'BURGLAR ALARM', 'DOMESTIC', 'CARDIAC RESP AR', 'PROPERTY FOUND',\n",
514
+ " 'ASSAULT', 'FIREWORKS', 'MISSING/RUNAWAY', 'OVERDOSE',\n",
515
+ " 'SEXUAL OFFENSE', 'MENTAL DISORDER', 'CHECK WELL BEIN',\n",
516
+ " 'SUSPICIOUS COND', 'PSYCHIATRIC', 'OPEN DOOR', 'ABANDONED AUTO',\n",
517
+ " 'HARASSMENT THRE', 'TRAFFIC VIOLATI', 'ANIMAL BITE',\n",
518
+ " 'LARCENY OF BIKE', 'SOLICITATION', 'JUVENILE RELATE',\n",
519
+ " 'ASSIST MOTORIST', 'ANIMAL', 'ANIMAL CALL', 'HAZARDOUS DRIVI',\n",
520
+ " 'LARCENY FROM RE', 'LARCENY OF AUTO', 'MVC', 'SUICIDE ATTEMPT',\n",
521
+ " 'GAS LEAK FIRE', 'MISSING PERSON', 'ASSIST OTHER AG',\n",
522
+ " 'PUBLICE SERVICE', 'DOMESTIC ASSIST', 'BURGLARY ATTEMP',\n",
523
+ " 'SUSPICIOUS VEHI', 'STAB GUNSHOT PE', 'UNKNOWN LE',\n",
524
+ " 'ROBBERY/CARJACK', 'MOTOR VEHICLE C', 'ALARMS', '911 HANGUP',\n",
525
+ " 'STRUCTURE FIRE', 'ABUSE/ABANDOMEN', 'VEHICLE FIRE', 'EXPLOSION',\n",
526
+ " 'DECEASED PERSON', 'DRIVING UNDER I', 'GUNSHOT INJURY',\n",
527
+ " 'SCHOOL PATROL', 'ACTIVE ASSAILAN', 'BOMB/CBRN/PRODU',\n",
528
+ " 'STATIONARY PATR', 'LITTERING', 'HOUSE CHECK', 'STAB GSW OR PEN',\n",
529
+ " 'CARDIAC', 'CLOSE PATROL', 'BOMB FOUND/SUSP', 'STRUCTURE COLLA',\n",
530
+ " 'INFO FOR ALL UN', 'MVC ENTRAPMENT', 'UNCONCIOUS OR F',\n",
531
+ " 'LIFTING ASSISTA', 'ATTEMPT TO LOCA', 'SICK PERSON',\n",
532
+ " 'HEAT OR COLD EX', 'CONFINED SPACE', 'TRAUMATIC INJUR',\n",
533
+ " 'MVC W INJURY AB', 'MVC W INJURY DE', 'DROWNING', 'CITY ORDINANCE'],\n",
534
+ " dtype=object)"
535
+ ]
536
+ },
537
+ "execution_count": 95,
538
+ "metadata": {},
539
+ "output_type": "execute_result"
540
+ }
541
+ ],
542
+ "source": [
543
+ "Chapel['Reported_As'].unique()"
544
+ ]
545
+ },
546
+ {
547
+ "cell_type": "code",
548
+ "execution_count": 96,
549
+ "id": "11ff26fb",
550
+ "metadata": {},
551
+ "outputs": [
552
+ {
553
+ "data": {
554
+ "text/plain": [
555
+ "array(['<Null>', 'HANDGUN', 'UNKNOWN', 'PERSONAL WEAPONS', 'NONE',\n",
556
+ " 'SHOTGUN', 'OTHER FIREARM', 'OTHER', 'RIFLE', 'UNARMED',\n",
557
+ " 'FIREARM (TYP NOT STATED)', 'KNIFE/CUTTING INSTRUMENT',\n",
558
+ " 'DRUGS/NARCOTICS/SLEEPING PILLS', 'BLUNT OBJECT',\n",
559
+ " 'FIREARM (TYPE NOT STATED)', 'MOTOR VEHICLE',\n",
560
+ " 'FIRE/INCENDIARY DEVICE', 'POISON', 'EXPLOSIVES', nan,\n",
561
+ " 'ASPHYXIATION'], dtype=object)"
562
+ ]
563
+ },
564
+ "execution_count": 96,
565
+ "metadata": {},
566
+ "output_type": "execute_result"
567
+ }
568
+ ],
569
+ "source": [
570
+ "Chapel['Weapon_Description'].unique()"
571
+ ]
572
+ },
573
+ {
574
+ "cell_type": "code",
575
+ "execution_count": 97,
576
+ "id": "245de78f",
577
+ "metadata": {},
578
+ "outputs": [],
579
+ "source": [
580
+ "# Replace specified values with None\n",
581
+ "replace_values = {'<Null>': None, 'NONE': None}\n",
582
+ "Chapel['Weapon_Description'] = Chapel['Weapon_Description'].replace(replace_values)"
583
+ ]
584
+ },
585
+ {
586
+ "cell_type": "code",
587
+ "execution_count": 98,
588
+ "id": "f3bca6d3",
589
+ "metadata": {},
590
+ "outputs": [
591
+ {
592
+ "data": {
593
+ "text/html": [
594
+ "<div>\n",
595
+ "<style scoped>\n",
596
+ " .dataframe tbody tr th:only-of-type {\n",
597
+ " vertical-align: middle;\n",
598
+ " }\n",
599
+ "\n",
600
+ " .dataframe tbody tr th {\n",
601
+ " vertical-align: top;\n",
602
+ " }\n",
603
+ "\n",
604
+ " .dataframe thead th {\n",
605
+ " text-align: right;\n",
606
+ " }\n",
607
+ "</style>\n",
608
+ "<table border=\"1\" class=\"dataframe\">\n",
609
+ " <thead>\n",
610
+ " <tr style=\"text-align: right;\">\n",
611
+ " <th></th>\n",
612
+ " <th>year</th>\n",
613
+ " <th>city</th>\n",
614
+ " <th>crime_major_category</th>\n",
615
+ " <th>crime_detail</th>\n",
616
+ " <th>latitude</th>\n",
617
+ " <th>longitude</th>\n",
618
+ " <th>occurance_time</th>\n",
619
+ " <th>clear_status</th>\n",
620
+ " <th>incident_address</th>\n",
621
+ " <th>notes</th>\n",
622
+ " </tr>\n",
623
+ " </thead>\n",
624
+ " <tbody>\n",
625
+ " <tr>\n",
626
+ " <th>101855</th>\n",
627
+ " <td>2023</td>\n",
628
+ " <td>Chapel Hill</td>\n",
629
+ " <td>Disorderly Conduct</td>\n",
630
+ " <td>Assist Carrboro</td>\n",
631
+ " <td>35.90967</td>\n",
632
+ " <td>-79.06506</td>\n",
633
+ " <td>2023/06/24 20:43:00</td>\n",
634
+ " <td>No Data</td>\n",
635
+ " <td>W FRANKLIN ST N MERRITT MILL RD</td>\n",
636
+ " <td>Weapon: None</td>\n",
637
+ " </tr>\n",
638
+ " <tr>\n",
639
+ " <th>101856</th>\n",
640
+ " <td>2023</td>\n",
641
+ " <td>Chapel Hill</td>\n",
642
+ " <td>Theft</td>\n",
643
+ " <td>Larceny From Motor Vehicle</td>\n",
644
+ " <td>35.92758</td>\n",
645
+ " <td>-79.03022</td>\n",
646
+ " <td>2023/09/12 17:45:00</td>\n",
647
+ " <td>No Data</td>\n",
648
+ " <td>WILLOW DR S ESTES DR</td>\n",
649
+ " <td>Weapon: None</td>\n",
650
+ " </tr>\n",
651
+ " <tr>\n",
652
+ " <th>101857</th>\n",
653
+ " <td>2023</td>\n",
654
+ " <td>Chapel Hill</td>\n",
655
+ " <td>Traffic Violations</td>\n",
656
+ " <td>Information</td>\n",
657
+ " <td>35.96392</td>\n",
658
+ " <td>-79.06455</td>\n",
659
+ " <td>2023/10/03 15:59:00</td>\n",
660
+ " <td>No Data</td>\n",
661
+ " <td>WEAVER DAIRY RD EXT PALAFOX DR</td>\n",
662
+ " <td>Weapon: None</td>\n",
663
+ " </tr>\n",
664
+ " <tr>\n",
665
+ " <th>101858</th>\n",
666
+ " <td>2023</td>\n",
667
+ " <td>Chapel Hill</td>\n",
668
+ " <td>Assault</td>\n",
669
+ " <td>Disturbing The Peace</td>\n",
670
+ " <td>35.91316</td>\n",
671
+ " <td>-79.05578</td>\n",
672
+ " <td>2023/10/27 05:40:00</td>\n",
673
+ " <td>No Data</td>\n",
674
+ " <td>W FRANKLIN ST N COLUMBIA ST</td>\n",
675
+ " <td>Weapon: Knife/Cutting Instrument</td>\n",
676
+ " </tr>\n",
677
+ " <tr>\n",
678
+ " <th>101859</th>\n",
679
+ " <td>2023</td>\n",
680
+ " <td>Chapel Hill</td>\n",
681
+ " <td>Miscellaneous</td>\n",
682
+ " <td>Lost Property</td>\n",
683
+ " <td>35.91137</td>\n",
684
+ " <td>-79.06041</td>\n",
685
+ " <td>2023/05/20 22:30:00</td>\n",
686
+ " <td>No Data</td>\n",
687
+ " <td>W FRANKLIN STY</td>\n",
688
+ " <td>Weapon: None</td>\n",
689
+ " </tr>\n",
690
+ " </tbody>\n",
691
+ "</table>\n",
692
+ "</div>"
693
+ ],
694
+ "text/plain": [
695
+ " year city crime_major_category crime_detail \\\n",
696
+ "101855 2023 Chapel Hill Disorderly Conduct Assist Carrboro \n",
697
+ "101856 2023 Chapel Hill Theft Larceny From Motor Vehicle \n",
698
+ "101857 2023 Chapel Hill Traffic Violations Information \n",
699
+ "101858 2023 Chapel Hill Assault Disturbing The Peace \n",
700
+ "101859 2023 Chapel Hill Miscellaneous Lost Property \n",
701
+ "\n",
702
+ " latitude longitude occurance_time clear_status \\\n",
703
+ "101855 35.90967 -79.06506 2023/06/24 20:43:00 No Data \n",
704
+ "101856 35.92758 -79.03022 2023/09/12 17:45:00 No Data \n",
705
+ "101857 35.96392 -79.06455 2023/10/03 15:59:00 No Data \n",
706
+ "101858 35.91316 -79.05578 2023/10/27 05:40:00 No Data \n",
707
+ "101859 35.91137 -79.06041 2023/05/20 22:30:00 No Data \n",
708
+ "\n",
709
+ " incident_address notes \n",
710
+ "101855 W FRANKLIN ST N MERRITT MILL RD Weapon: None \n",
711
+ "101856 WILLOW DR S ESTES DR Weapon: None \n",
712
+ "101857 WEAVER DAIRY RD EXT PALAFOX DR Weapon: None \n",
713
+ "101858 W FRANKLIN ST N COLUMBIA ST Weapon: Knife/Cutting Instrument \n",
714
+ "101859 W FRANKLIN STY Weapon: None "
715
+ ]
716
+ },
717
+ "execution_count": 98,
718
+ "metadata": {},
719
+ "output_type": "execute_result"
720
+ }
721
+ ],
722
+ "source": [
723
+ "# Create a mapping for crime categories\n",
724
+ "category_mapping = {\n",
725
+ " '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",
726
+ " 'Assault': ['ASSAULT/SEXUAL', 'ASSAULT', 'STAB GUNSHOT PE', 'ACTIVE ASSAILAN'],\n",
727
+ " 'Burglary': ['BURGLARY', 'BURGLARY ATTEMP', 'STRUCTURE COLLAPSE', 'ROBBERY/CARJACK'],\n",
728
+ " 'Drugs': ['DRUGS'],\n",
729
+ " 'Traffic Violations': ['TRAFFIC STOP', 'TRAFFIC/TRANSPO', 'TRAFFIC VIOLATI', 'MVC', 'MVC W INJURY', 'MVC W INJURY AB', 'MVC W INJURY DE', 'MVC ENTRAPMENT'],\n",
730
+ " 'Disorderly Conduct': ['DISTURBANCE/NUI', 'DOMESTIC DISTUR', 'DISPUTE', 'DISTURBANCE', 'LOST PROPERTY', 'TRESPASSING/UNW', 'REFUSAL TO LEAV', 'SUSPICIOUS COND', 'STRUCTURE FIRE'],\n",
731
+ " 'Fraud': ['FRAUD OR DECEPT'],\n",
732
+ " 'Sexual Offenses': ['SEXUAL OFFENSE'],\n",
733
+ " 'Homicide': ['SUICIDE ATTEMPT', 'ABUSE/ABANDOMEN', 'DECEASED PERSON'],\n",
734
+ " 'Weapons Violations': ['WEAPON/FIREARMS'],\n",
735
+ " 'Animal-related Offenses': ['ANIMAL BITE', 'ANIMAL', 'ANIMAL CALL'],\n",
736
+ " 'Missing Person': ['MISSING PERSON'],\n",
737
+ " 'Public Service': ['PUBLIC SERVICE', 'PUBLICE SERVICE'],\n",
738
+ " '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",
739
+ "}\n",
740
+ "\n",
741
+ "\n",
742
+ "# Function to categorize crime based on the mapping dictionary\n",
743
+ "def categorize_crime(crime):\n",
744
+ " for category, crimes in category_mapping.items():\n",
745
+ " if crime in crimes:\n",
746
+ " return category\n",
747
+ " return 'Miscellaneous'\n",
748
+ "\n",
749
+ "# Create a new DataFrame with simplified crime categories\n",
750
+ "Chapel_new = pd.DataFrame({\n",
751
+ " \"year\": pd.to_datetime(Chapel['Date_of_Occurrence']).dt.year,\n",
752
+ " \"city\": \"Chapel Hill\",\n",
753
+ " \"crime_major_category\": Chapel['Reported_As'].apply(categorize_crime),\n",
754
+ " \"crime_detail\": Chapel['Offense'].str.title(),\n",
755
+ " \"latitude\": Chapel['X'].round(5).fillna(0),\n",
756
+ " \"longitude\": Chapel['Y'].round(5).fillna(0),\n",
757
+ " \"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",
758
+ " \"clear_status\": None,\n",
759
+ " \"incident_address\": Chapel['Street'].str.replace(\"@\", \" \"),\n",
760
+ " \"notes\": Chapel['Weapon_Description'].apply(lambda x: f\"Weapon: {x}\" if pd.notnull(x) else \"Weapon: None\").str.title()\n",
761
+ "}).fillna(\"No Data\")\n",
762
+ "\n",
763
+ "Chapel_new = Chapel_new[Chapel_new['year'] >= 2015]\n",
764
+ "Chapel_new.tail()"
765
+ ]
766
+ },
767
+ {
768
+ "cell_type": "code",
769
+ "execution_count": 100,
770
+ "id": "303148b6",
771
+ "metadata": {},
772
+ "outputs": [
773
+ {
774
+ "data": {
775
+ "text/plain": [
776
+ "(67010, 10)"
777
+ ]
778
+ },
779
+ "execution_count": 100,
780
+ "metadata": {},
781
+ "output_type": "execute_result"
782
+ }
783
+ ],
784
+ "source": [
785
+ "Chapel_new.shape"
786
+ ]
787
+ },
788
+ {
789
+ "cell_type": "markdown",
790
+ "id": "80403d93",
791
+ "metadata": {},
792
+ "source": [
793
+ "# Charlotte"
794
+ ]
795
+ },
796
+ {
797
+ "cell_type": "code",
798
+ "execution_count": 101,
799
+ "id": "c8a782e3",
800
+ "metadata": {},
801
+ "outputs": [],
802
+ "source": [
803
+ "import pandas as pd\n",
804
+ "Charlotte = pd.read_csv(\"Charlotte.csv\", low_memory = False)"
805
+ ]
806
+ },
807
+ {
808
+ "cell_type": "markdown",
809
+ "id": "65e574af",
810
+ "metadata": {},
811
+ "source": [
812
+ "## Exploratory Data Analysis"
813
+ ]
814
+ },
815
+ {
816
+ "cell_type": "code",
817
+ "execution_count": 102,
818
+ "id": "2014125e",
819
+ "metadata": {},
820
+ "outputs": [
821
+ {
822
+ "data": {
823
+ "text/plain": [
824
+ "array(['Open', 'Cleared by Other Means', 'Victim Chose not to Prosecute',\n",
825
+ " 'Cleared by Arrest', 'Located (Missing Persons and Runaways only)',\n",
826
+ " 'Unfounded', 'Unfounded-Referred to Other Agency',\n",
827
+ " 'Cleared, Pending Arrest Validation',\n",
828
+ " 'Cleared by Arrest by Another Agency',\n",
829
+ " 'Prosecution Declined by DA', 'By Death of Offender',\n",
830
+ " 'Extradition Declined'], dtype=object)"
831
+ ]
832
+ },
833
+ "execution_count": 102,
834
+ "metadata": {},
835
+ "output_type": "execute_result"
836
+ }
837
+ ],
838
+ "source": [
839
+ "Charlotte['CLEARANCE_DETAIL_STATUS'].unique()"
840
+ ]
841
+ },
842
+ {
843
+ "cell_type": "code",
844
+ "execution_count": 103,
845
+ "id": "8c68d990",
846
+ "metadata": {},
847
+ "outputs": [
848
+ {
849
+ "data": {
850
+ "text/plain": [
851
+ "Index(['X', 'Y', 'YEAR', 'INCIDENT_REPORT_ID', 'LOCATION', 'CITY', 'STATE',\n",
852
+ " 'ZIP', 'X_COORD_PUBLIC', 'Y_COORD_PUBLIC', 'LATITUDE_PUBLIC',\n",
853
+ " 'LONGITUDE_PUBLIC', 'DIVISION_ID', 'CMPD_PATROL_DIVISION', 'NPA',\n",
854
+ " 'DATE_REPORTED', 'DATE_INCIDENT_BEGAN', 'DATE_INCIDENT_END',\n",
855
+ " 'ADDRESS_DESCRIPTION', 'LOCATION_TYPE_DESCRIPTION',\n",
856
+ " 'PLACE_TYPE_DESCRIPTION', 'PLACE_DETAIL_DESCRIPTION',\n",
857
+ " 'CLEARANCE_STATUS', 'CLEARANCE_DETAIL_STATUS', 'CLEARANCE_DATE',\n",
858
+ " 'HIGHEST_NIBRS_CODE', 'HIGHEST_NIBRS_DESCRIPTION', 'OBJECTID', 'Shape',\n",
859
+ " 'GlobalID'],\n",
860
+ " dtype='object')"
861
+ ]
862
+ },
863
+ "execution_count": 103,
864
+ "metadata": {},
865
+ "output_type": "execute_result"
866
+ }
867
+ ],
868
+ "source": [
869
+ "Charlotte.columns"
870
+ ]
871
+ },
872
+ {
873
+ "cell_type": "code",
874
+ "execution_count": 104,
875
+ "id": "16b564ce",
876
+ "metadata": {},
877
+ "outputs": [
878
+ {
879
+ "data": {
880
+ "text/plain": [
881
+ "array(['Identity Theft', 'All Other Offenses', 'Theft From Motor Vehicle',\n",
882
+ " 'Shoplifting', 'All Other Thefts', 'Credit Card/Teller Fraud',\n",
883
+ " 'Forcible Fondling', 'Damage/Vandalism Of Property',\n",
884
+ " 'Intimidation', 'Drug/Narcotic Violations',\n",
885
+ " 'Other Unlisted Non-Criminal', 'Simple Assault',\n",
886
+ " 'Motor Vehicle Theft', 'Missing Person', 'Embezzlement',\n",
887
+ " 'Forcible Rape', 'Aggravated Assault', 'Burglary/B&E',\n",
888
+ " 'Theft From Building', 'Impersonation', 'False Pretenses/Swindle',\n",
889
+ " 'Extortion/Blackmail', 'Arson', 'Vehicle Recovery', 'Suicide',\n",
890
+ " 'Sudden/Natural Death Investigation', 'Affray',\n",
891
+ " 'Weapon Law Violations', 'Trespass Of Real Property', 'Murder',\n",
892
+ " 'Driving Under The Influence', 'Stolen Property Offenses',\n",
893
+ " 'Robbery', 'Overdose', 'Theft of Motor Vehicle Parts from Vehicle',\n",
894
+ " 'Hacking/Computer Invasion', 'Counterfeiting/Forgery',\n",
895
+ " 'Kidnapping', 'Drug Equipment Violations',\n",
896
+ " 'Fire (Accidental/Non-Arson)', 'Pornography/Obscene Material',\n",
897
+ " 'Disorderly Conduct', 'Indecent Exposure', 'Public Accident',\n",
898
+ " 'Pocket-Picking', 'Peeping Tom', 'Purse-Snatching',\n",
899
+ " 'Liquor Law Violations', 'Statutory Rape', 'Wire Fraud',\n",
900
+ " 'Family Offenses; Nonviolent', 'Incest', 'Forcible Sodomy',\n",
901
+ " 'Theft From Coin-Operated Machine Or Device',\n",
902
+ " 'Human Trafficking, Involuntary Servitude',\n",
903
+ " 'Human Trafficking, Commercial Sex Acts', 'Justifiable Homicide',\n",
904
+ " 'Curfew/Loitering/Vagrancy Violations', 'Negligent Manslaughter',\n",
905
+ " 'Worthless Check: Felony (over $2000)', 'Animal Cruelty',\n",
906
+ " 'Sexual Assault With Object', 'Assisting Gambling',\n",
907
+ " 'Dog Bite/Animal Control Incident', 'Prostitution',\n",
908
+ " 'Assisting Prostitution', 'Gambling Equipment Violations',\n",
909
+ " 'Betting/Wagering', 'Welfare Fraud', 'Purchasing Prostitution',\n",
910
+ " 'Gas Leak', 'Bribery'], dtype=object)"
911
+ ]
912
+ },
913
+ "execution_count": 104,
914
+ "metadata": {},
915
+ "output_type": "execute_result"
916
+ }
917
+ ],
918
+ "source": [
919
+ "Charlotte['HIGHEST_NIBRS_DESCRIPTION'].unique()"
920
+ ]
921
+ },
922
+ {
923
+ "cell_type": "code",
924
+ "execution_count": 128,
925
+ "id": "f5adcb4c",
926
+ "metadata": {},
927
+ "outputs": [
928
+ {
929
+ "data": {
930
+ "text/html": [
931
+ "<div>\n",
932
+ "<style scoped>\n",
933
+ " .dataframe tbody tr th:only-of-type {\n",
934
+ " vertical-align: middle;\n",
935
+ " }\n",
936
+ "\n",
937
+ " .dataframe tbody tr th {\n",
938
+ " vertical-align: top;\n",
939
+ " }\n",
940
+ "\n",
941
+ " .dataframe thead th {\n",
942
+ " text-align: right;\n",
943
+ " }\n",
944
+ "</style>\n",
945
+ "<table border=\"1\" class=\"dataframe\">\n",
946
+ " <thead>\n",
947
+ " <tr style=\"text-align: right;\">\n",
948
+ " <th></th>\n",
949
+ " <th>year</th>\n",
950
+ " <th>city</th>\n",
951
+ " <th>crime_major_category</th>\n",
952
+ " <th>crime_detail</th>\n",
953
+ " <th>latitude</th>\n",
954
+ " <th>longitude</th>\n",
955
+ " <th>occurance_time</th>\n",
956
+ " <th>clear_status</th>\n",
957
+ " <th>incident_address</th>\n",
958
+ " <th>notes</th>\n",
959
+ " </tr>\n",
960
+ " </thead>\n",
961
+ " <tbody>\n",
962
+ " <tr>\n",
963
+ " <th>661137</th>\n",
964
+ " <td>2017</td>\n",
965
+ " <td>Charlotte</td>\n",
966
+ " <td>Assault</td>\n",
967
+ " <td>Aggravated Assault</td>\n",
968
+ " <td>35.22712</td>\n",
969
+ " <td>-80.73457</td>\n",
970
+ " <td>2017/01/01 00:00:00</td>\n",
971
+ " <td>Open</td>\n",
972
+ " <td>4400 GAYNELLE DR</td>\n",
973
+ " <td>Location Type Description: Indoors; Place Type...</td>\n",
974
+ " </tr>\n",
975
+ " <tr>\n",
976
+ " <th>661138</th>\n",
977
+ " <td>2017</td>\n",
978
+ " <td>Charlotte</td>\n",
979
+ " <td>Miscellaneous</td>\n",
980
+ " <td>Damage/Vandalism Of Property</td>\n",
981
+ " <td>35.25992</td>\n",
982
+ " <td>-80.89929</td>\n",
983
+ " <td>2017/01/01 00:00:00</td>\n",
984
+ " <td>Open</td>\n",
985
+ " <td>1100 MARBLE ST</td>\n",
986
+ " <td>Location Type Description: Indoors; Place Type...</td>\n",
987
+ " </tr>\n",
988
+ " <tr>\n",
989
+ " <th>661139</th>\n",
990
+ " <td>2017</td>\n",
991
+ " <td>Charlotte</td>\n",
992
+ " <td>Assault</td>\n",
993
+ " <td>Simple Assault</td>\n",
994
+ " <td>35.05221</td>\n",
995
+ " <td>-80.85127</td>\n",
996
+ " <td>2017/01/01 00:00:00</td>\n",
997
+ " <td>Cleared by Arrest</td>\n",
998
+ " <td>14800 BALLANTYNE VILLAGE WY</td>\n",
999
+ " <td>Location Type Description: Outdoors; Place Typ...</td>\n",
1000
+ " </tr>\n",
1001
+ " <tr>\n",
1002
+ " <th>661140</th>\n",
1003
+ " <td>2017</td>\n",
1004
+ " <td>Charlotte</td>\n",
1005
+ " <td>Miscellaneous</td>\n",
1006
+ " <td>Damage/Vandalism Of Property</td>\n",
1007
+ " <td>35.26302</td>\n",
1008
+ " <td>-80.84583</td>\n",
1009
+ " <td>2017/01/01 00:00:00</td>\n",
1010
+ " <td>Open</td>\n",
1011
+ " <td>2100 KENNESAW DR</td>\n",
1012
+ " <td>Location Type Description: Indoors; Place Type...</td>\n",
1013
+ " </tr>\n",
1014
+ " <tr>\n",
1015
+ " <th>661141</th>\n",
1016
+ " <td>2017</td>\n",
1017
+ " <td>Charlotte</td>\n",
1018
+ " <td>Disorderly Conduct</td>\n",
1019
+ " <td>Disorderly Conduct</td>\n",
1020
+ " <td>35.20613</td>\n",
1021
+ " <td>-80.79514</td>\n",
1022
+ " <td>2017/01/01 00:00:00</td>\n",
1023
+ " <td>Cleared by Arrest</td>\n",
1024
+ " <td>2700 E INDEPENDENCE BV</td>\n",
1025
+ " <td>Location Type Description: Other; Place Type D...</td>\n",
1026
+ " </tr>\n",
1027
+ " </tbody>\n",
1028
+ "</table>\n",
1029
+ "</div>"
1030
+ ],
1031
+ "text/plain": [
1032
+ " year city crime_major_category crime_detail \\\n",
1033
+ "661137 2017 Charlotte Assault Aggravated Assault \n",
1034
+ "661138 2017 Charlotte Miscellaneous Damage/Vandalism Of Property \n",
1035
+ "661139 2017 Charlotte Assault Simple Assault \n",
1036
+ "661140 2017 Charlotte Miscellaneous Damage/Vandalism Of Property \n",
1037
+ "661141 2017 Charlotte Disorderly Conduct Disorderly Conduct \n",
1038
+ "\n",
1039
+ " latitude longitude occurance_time clear_status \\\n",
1040
+ "661137 35.22712 -80.73457 2017/01/01 00:00:00 Open \n",
1041
+ "661138 35.25992 -80.89929 2017/01/01 00:00:00 Open \n",
1042
+ "661139 35.05221 -80.85127 2017/01/01 00:00:00 Cleared by Arrest \n",
1043
+ "661140 35.26302 -80.84583 2017/01/01 00:00:00 Open \n",
1044
+ "661141 35.20613 -80.79514 2017/01/01 00:00:00 Cleared by Arrest \n",
1045
+ "\n",
1046
+ " incident_address \\\n",
1047
+ "661137 4400 GAYNELLE DR \n",
1048
+ "661138 1100 MARBLE ST \n",
1049
+ "661139 14800 BALLANTYNE VILLAGE WY \n",
1050
+ "661140 2100 KENNESAW DR \n",
1051
+ "661141 2700 E INDEPENDENCE BV \n",
1052
+ "\n",
1053
+ " notes \n",
1054
+ "661137 Location Type Description: Indoors; Place Type... \n",
1055
+ "661138 Location Type Description: Indoors; Place Type... \n",
1056
+ "661139 Location Type Description: Outdoors; Place Typ... \n",
1057
+ "661140 Location Type Description: Indoors; Place Type... \n",
1058
+ "661141 Location Type Description: Other; Place Type D... "
1059
+ ]
1060
+ },
1061
+ "execution_count": 128,
1062
+ "metadata": {},
1063
+ "output_type": "execute_result"
1064
+ }
1065
+ ],
1066
+ "source": [
1067
+ "df = Charlotte\n",
1068
+ "df = Charlotte\n",
1069
+ "category_mapping = {\n",
1070
+ " 'Theft': [\n",
1071
+ " 'Identity Theft', 'Theft From Motor Vehicle', 'Shoplifting', 'All Other Thefts',\n",
1072
+ " 'Credit Card/Teller Fraud', 'Motor Vehicle Theft', 'Theft From Building',\n",
1073
+ " 'Theft of Motor Vehicle Parts from Vehicle', 'Purse-Snatching', 'Pocket-Picking',\n",
1074
+ " 'Theft From Coin-Operated Machine Or Device'\n",
1075
+ " ],\n",
1076
+ " 'Fraud': [\n",
1077
+ " 'Credit Card/Teller Fraud', 'Embezzlement', 'Impersonation', 'False Pretenses/Swindle',\n",
1078
+ " 'Extortion/Blackmail', 'Counterfeiting/Forgery', 'Welfare Fraud', 'Wire Fraud',\n",
1079
+ " 'Hacking/Computer Invasion'\n",
1080
+ " ],\n",
1081
+ " 'Assault': [\n",
1082
+ " 'Forcible Fondling', 'Simple Assault', 'Aggravated Assault', 'Forcible Rape',\n",
1083
+ " 'Intimidation', 'Sexual Assault With Object', 'Statutory Rape', 'Incest',\n",
1084
+ " 'Forcible Sodomy', 'Affray'\n",
1085
+ " ],\n",
1086
+ " 'Drugs': [\n",
1087
+ " 'Drug/Narcotic Violations', 'Drug Equipment Violations'\n",
1088
+ " ],\n",
1089
+ " 'Sexual Offenses': [\n",
1090
+ " 'Forcible Rape', 'Forcible Fondling', 'Sexual Assault With Object',\n",
1091
+ " 'Indecent Exposure', 'Peeping Tom', 'Statutory Rape', 'Incest', 'Forcible Sodomy'\n",
1092
+ " ],\n",
1093
+ " 'Homicide': [\n",
1094
+ " 'Murder', 'Justifiable Homicide', 'Negligent Manslaughter'\n",
1095
+ " ],\n",
1096
+ " 'Arson': [\n",
1097
+ " 'Arson', 'Fire (Accidental/Non-Arson)'\n",
1098
+ " ],\n",
1099
+ " 'Kidnapping': [\n",
1100
+ " 'Kidnapping'\n",
1101
+ " ],\n",
1102
+ " 'Weapons Violations': [\n",
1103
+ " 'Weapon Law Violations'\n",
1104
+ " ],\n",
1105
+ " 'Traffic Violations': [\n",
1106
+ " 'Driving Under The Influence'\n",
1107
+ " ],\n",
1108
+ " 'Disorderly Conduct': [\n",
1109
+ " 'Disorderly Conduct', 'Public Accident'\n",
1110
+ " ],\n",
1111
+ " 'Gambling': [\n",
1112
+ " 'Assisting Gambling', 'Gambling Equipment Violations', 'Betting/Wagering'\n",
1113
+ " ],\n",
1114
+ " 'Animal-related Offenses': [\n",
1115
+ " 'Animal Cruelty', 'Dog Bite/Animal Control Incident'\n",
1116
+ " ],\n",
1117
+ " 'Prostitution-related Offenses': [\n",
1118
+ " 'Prostitution', 'Assisting Prostitution', 'Purchasing Prostitution'\n",
1119
+ " ]\n",
1120
+ "}\n",
1121
+ "\n",
1122
+ "# Function to categorize crime based on the mapping dictionary\n",
1123
+ "def categorize_crime(crime):\n",
1124
+ " for category, crimes in category_mapping.items():\n",
1125
+ " if crime in crimes:\n",
1126
+ " return category\n",
1127
+ " return 'Miscellaneous'\n",
1128
+ "\n",
1129
+ "Charlotte_new = pd.DataFrame({\n",
1130
+ " \"year\": df['YEAR'],\n",
1131
+ " \"city\": \"Charlotte\",\n",
1132
+ " \"crime_major_category\": df['HIGHEST_NIBRS_DESCRIPTION'].apply(categorize_crime),\n",
1133
+ " \"crime_detail\": df['HIGHEST_NIBRS_DESCRIPTION'],\n",
1134
+ " \"latitude\": df['LATITUDE_PUBLIC'].round(5).fillna(0),\n",
1135
+ " \"longitude\": df['LONGITUDE_PUBLIC'].round(5).fillna(0),\n",
1136
+ " \"occurance_time\": pd.to_datetime(df['DATE_INCIDENT_BEGAN'].str.replace(r'\\+\\d{2}$', '', regex=True), errors='coerce').dt.strftime('%Y/%m/%d %H:%M:%S'),\n",
1137
+ " \"clear_status\": df['CLEARANCE_DETAIL_STATUS'],\n",
1138
+ " \"incident_address\": df['LOCATION'],\n",
1139
+ " \"notes\": 'Location Type Description: ' + df['LOCATION_TYPE_DESCRIPTION'].str.title().str.replace('_', ' ') + '; ' +\n",
1140
+ " 'Place Type Description: ' + df['PLACE_TYPE_DESCRIPTION'].str.title().str.replace('_', ' ') + '; ' +\n",
1141
+ " 'Place Detail Description: ' + df['PLACE_DETAIL_DESCRIPTION'].str.title().str.replace('_', ' ')\n",
1142
+ "})\n",
1143
+ "\n",
1144
+ "Charlotte_new.dropna(subset=['occurance_time'], inplace=True)\n",
1145
+ "Charlotte_new.fillna(\"No Data\", inplace=True)\n",
1146
+ "Charlotte_new.tail()"
1147
+ ]
1148
+ },
1149
+ {
1150
+ "cell_type": "code",
1151
+ "execution_count": 129,
1152
+ "id": "1026993f",
1153
+ "metadata": {},
1154
+ "outputs": [
1155
+ {
1156
+ "data": {
1157
+ "text/plain": [
1158
+ "(661086, 10)"
1159
+ ]
1160
+ },
1161
+ "execution_count": 129,
1162
+ "metadata": {},
1163
+ "output_type": "execute_result"
1164
+ }
1165
+ ],
1166
+ "source": [
1167
+ "Charlotte_new.shape"
1168
+ ]
1169
+ },
1170
+ {
1171
+ "cell_type": "markdown",
1172
+ "id": "ab448155",
1173
+ "metadata": {},
1174
+ "source": [
1175
+ "# Cary"
1176
+ ]
1177
+ },
1178
+ {
1179
+ "cell_type": "code",
1180
+ "execution_count": 107,
1181
+ "id": "ec11f03c",
1182
+ "metadata": {},
1183
+ "outputs": [],
1184
+ "source": [
1185
+ "import pandas as pd\n",
1186
+ "Cary = pd.read_csv(\"Cary.csv\", low_memory = False).dropna(subset=['Year'])"
1187
+ ]
1188
+ },
1189
+ {
1190
+ "cell_type": "markdown",
1191
+ "id": "194aec45",
1192
+ "metadata": {},
1193
+ "source": [
1194
+ "## Exploratory Data Analysis"
1195
+ ]
1196
+ },
1197
+ {
1198
+ "cell_type": "code",
1199
+ "execution_count": 108,
1200
+ "id": "5c5249e1",
1201
+ "metadata": {},
1202
+ "outputs": [
1203
+ {
1204
+ "data": {
1205
+ "text/plain": [
1206
+ "Index(['Crime Category', 'Crime Type', 'UCR', 'Map Reference',\n",
1207
+ " 'Incident Number', 'Begin Date Of Occurrence',\n",
1208
+ " 'Begin Time Of Occurrence', 'End Date Of Occurrence',\n",
1209
+ " 'End Time Of Occurrence', 'Crime Day', 'Geo Code', 'Location Category',\n",
1210
+ " 'District', 'Beat Number', 'Location', 'ID', 'Lat', 'Lon',\n",
1211
+ " 'Charge Count', 'Neighborhood ID', 'Apartment Complex',\n",
1212
+ " 'Residential Subdivision', 'Subdivision ID', 'Phx Activity Date',\n",
1213
+ " 'Phx Record Status', 'Phx Community', 'Phx Status', 'Record',\n",
1214
+ " 'Offense Category', 'Violent Property', 'timeframe', 'domestic',\n",
1215
+ " 'Total Incidents', 'Year'],\n",
1216
+ " dtype='object')"
1217
+ ]
1218
+ },
1219
+ "execution_count": 108,
1220
+ "metadata": {},
1221
+ "output_type": "execute_result"
1222
+ }
1223
+ ],
1224
+ "source": [
1225
+ "Cary.columns"
1226
+ ]
1227
+ },
1228
+ {
1229
+ "cell_type": "code",
1230
+ "execution_count": 84,
1231
+ "id": "187a83c8",
1232
+ "metadata": {},
1233
+ "outputs": [
1234
+ {
1235
+ "data": {
1236
+ "text/plain": [
1237
+ "array(['ALL OTHER', 'ARSON', 'AGGRAVATED ASSAULT', 'BURGLARY',\n",
1238
+ " 'MOTOR VEHICLE THEFT', 'MURDER', 'ROBBERY', 'LARCENY'],\n",
1239
+ " dtype=object)"
1240
+ ]
1241
+ },
1242
+ "execution_count": 84,
1243
+ "metadata": {},
1244
+ "output_type": "execute_result"
1245
+ }
1246
+ ],
1247
+ "source": [
1248
+ "Cary['Crime Category'].unique()"
1249
+ ]
1250
+ },
1251
+ {
1252
+ "cell_type": "code",
1253
+ "execution_count": 110,
1254
+ "id": "9f7ade9d",
1255
+ "metadata": {},
1256
+ "outputs": [
1257
+ {
1258
+ "data": {
1259
+ "text/html": [
1260
+ "<div>\n",
1261
+ "<style scoped>\n",
1262
+ " .dataframe tbody tr th:only-of-type {\n",
1263
+ " vertical-align: middle;\n",
1264
+ " }\n",
1265
+ "\n",
1266
+ " .dataframe tbody tr th {\n",
1267
+ " vertical-align: top;\n",
1268
+ " }\n",
1269
+ "\n",
1270
+ " .dataframe thead th {\n",
1271
+ " text-align: right;\n",
1272
+ " }\n",
1273
+ "</style>\n",
1274
+ "<table border=\"1\" class=\"dataframe\">\n",
1275
+ " <thead>\n",
1276
+ " <tr style=\"text-align: right;\">\n",
1277
+ " <th></th>\n",
1278
+ " <th>year</th>\n",
1279
+ " <th>city</th>\n",
1280
+ " <th>crime_major_category</th>\n",
1281
+ " <th>crime_detail</th>\n",
1282
+ " <th>latitude</th>\n",
1283
+ " <th>longitude</th>\n",
1284
+ " <th>occurance_time</th>\n",
1285
+ " <th>clear_status</th>\n",
1286
+ " <th>incident_address</th>\n",
1287
+ " <th>notes</th>\n",
1288
+ " </tr>\n",
1289
+ " </thead>\n",
1290
+ " <tbody>\n",
1291
+ " <tr>\n",
1292
+ " <th>116510</th>\n",
1293
+ " <td>2022</td>\n",
1294
+ " <td>Cary</td>\n",
1295
+ " <td>Miscellaneous</td>\n",
1296
+ " <td>Missing Person</td>\n",
1297
+ " <td>35.79749</td>\n",
1298
+ " <td>-78.81358</td>\n",
1299
+ " <td>2022/11/13 13:01:36</td>\n",
1300
+ " <td>No Data</td>\n",
1301
+ " <td>EDENHURST AVE</td>\n",
1302
+ " <td>District: CpdnViolent Property: Non-Repor</td>\n",
1303
+ " </tr>\n",
1304
+ " <tr>\n",
1305
+ " <th>116546</th>\n",
1306
+ " <td>2022</td>\n",
1307
+ " <td>Cary</td>\n",
1308
+ " <td>Miscellaneous</td>\n",
1309
+ " <td>Missing Person</td>\n",
1310
+ " <td>35.78252</td>\n",
1311
+ " <td>-78.81503</td>\n",
1312
+ " <td>2022/10/15 15:00:00</td>\n",
1313
+ " <td>No Data</td>\n",
1314
+ " <td>BYRAMS FORD DR</td>\n",
1315
+ " <td>District: CpdsViolent Property: Non-Repor</td>\n",
1316
+ " </tr>\n",
1317
+ " <tr>\n",
1318
+ " <th>116552</th>\n",
1319
+ " <td>2022</td>\n",
1320
+ " <td>Cary</td>\n",
1321
+ " <td>Miscellaneous</td>\n",
1322
+ " <td>Missing Person</td>\n",
1323
+ " <td>35.76020</td>\n",
1324
+ " <td>-78.74427</td>\n",
1325
+ " <td>2022/11/24 12:00:00</td>\n",
1326
+ " <td>No Data</td>\n",
1327
+ " <td>WALNUT ST</td>\n",
1328
+ " <td>District: CpdsViolent Property: Non-Repor</td>\n",
1329
+ " </tr>\n",
1330
+ " <tr>\n",
1331
+ " <th>116558</th>\n",
1332
+ " <td>2018</td>\n",
1333
+ " <td>Cary</td>\n",
1334
+ " <td>Miscellaneous</td>\n",
1335
+ " <td>Missing Person</td>\n",
1336
+ " <td>35.82342</td>\n",
1337
+ " <td>-78.90523</td>\n",
1338
+ " <td>2018/05/10 05:00:00</td>\n",
1339
+ " <td>No Data</td>\n",
1340
+ " <td>EMERALD DOWNS RD</td>\n",
1341
+ " <td>District: D2Violent Property: Non-Repor</td>\n",
1342
+ " </tr>\n",
1343
+ " <tr>\n",
1344
+ " <th>116573</th>\n",
1345
+ " <td>2023</td>\n",
1346
+ " <td>Cary</td>\n",
1347
+ " <td>Miscellaneous</td>\n",
1348
+ " <td>Missing Person</td>\n",
1349
+ " <td>35.78005</td>\n",
1350
+ " <td>-78.75733</td>\n",
1351
+ " <td>2023/07/22 23:30:00</td>\n",
1352
+ " <td>No Data</td>\n",
1353
+ " <td>FENTON GATEWAY DR</td>\n",
1354
+ " <td>District: CpdsViolent Property: Non-Repor</td>\n",
1355
+ " </tr>\n",
1356
+ " </tbody>\n",
1357
+ "</table>\n",
1358
+ "</div>"
1359
+ ],
1360
+ "text/plain": [
1361
+ " year city crime_major_category crime_detail latitude longitude \\\n",
1362
+ "116510 2022 Cary Miscellaneous Missing Person 35.79749 -78.81358 \n",
1363
+ "116546 2022 Cary Miscellaneous Missing Person 35.78252 -78.81503 \n",
1364
+ "116552 2022 Cary Miscellaneous Missing Person 35.76020 -78.74427 \n",
1365
+ "116558 2018 Cary Miscellaneous Missing Person 35.82342 -78.90523 \n",
1366
+ "116573 2023 Cary Miscellaneous Missing Person 35.78005 -78.75733 \n",
1367
+ "\n",
1368
+ " occurance_time clear_status incident_address \\\n",
1369
+ "116510 2022/11/13 13:01:36 No Data EDENHURST AVE \n",
1370
+ "116546 2022/10/15 15:00:00 No Data BYRAMS FORD DR \n",
1371
+ "116552 2022/11/24 12:00:00 No Data WALNUT ST \n",
1372
+ "116558 2018/05/10 05:00:00 No Data EMERALD DOWNS RD \n",
1373
+ "116573 2023/07/22 23:30:00 No Data FENTON GATEWAY DR \n",
1374
+ "\n",
1375
+ " notes \n",
1376
+ "116510 District: CpdnViolent Property: Non-Repor \n",
1377
+ "116546 District: CpdsViolent Property: Non-Repor \n",
1378
+ "116552 District: CpdsViolent Property: Non-Repor \n",
1379
+ "116558 District: D2Violent Property: Non-Repor \n",
1380
+ "116573 District: CpdsViolent Property: Non-Repor "
1381
+ ]
1382
+ },
1383
+ "execution_count": 110,
1384
+ "metadata": {},
1385
+ "output_type": "execute_result"
1386
+ }
1387
+ ],
1388
+ "source": [
1389
+ "df = Cary\n",
1390
+ "def categorize_crime(crime):\n",
1391
+ " crime_mapping = {\n",
1392
+ " 'Theft': ['BURGLARY', 'MOTOR VEHICLE THEFT', 'LARCENY'],\n",
1393
+ " 'Arson': ['ARSON'],\n",
1394
+ " 'Assault': ['AGGRAVATED ASSAULT'],\n",
1395
+ " 'Homicide': ['MURDER'],\n",
1396
+ " 'Robbery': ['ROBBERY']\n",
1397
+ " }\n",
1398
+ "\n",
1399
+ " for category, crimes in crime_mapping.items():\n",
1400
+ " if crime in crimes:\n",
1401
+ " return category\n",
1402
+ " return 'Miscellaneous'\n",
1403
+ "\n",
1404
+ "Cary_new = pd.DataFrame({\n",
1405
+ " \"year\": df[\"Year\"].astype(int),\n",
1406
+ " \"city\": \"Cary\",\n",
1407
+ " \"crime_major_category\": df['Crime Category'].apply(categorize_crime).str.title(),\n",
1408
+ " \"crime_detail\": df['Crime Type'].str.title(),\n",
1409
+ " \"latitude\": df['Lat'].fillna(0).round(5).fillna(0),\n",
1410
+ " \"longitude\": df['Lon'].fillna(0).round(5).fillna(0),\n",
1411
+ " \"occurance_time\": pd.to_datetime(df['Begin Date Of Occurrence'] + ' ' + df['Begin Time Of Occurrence']).dt.strftime('%Y/%m/%d %H:%M:%S'),\n",
1412
+ " \"clear_status\": None,\n",
1413
+ " \"incident_address\": df['Geo Code'],\n",
1414
+ " \"notes\": 'District: '+ df['District'].str.title() + 'Violent Property: ' + df['Violent Property'].str.title()\n",
1415
+ "}).fillna(\"No Data\")\n",
1416
+ "\n",
1417
+ "Cary_new = Cary_new[Cary_new['year'] >= 2015]\n",
1418
+ "Cary_new.tail()\n"
1419
+ ]
1420
+ },
1421
+ {
1422
+ "cell_type": "code",
1423
+ "execution_count": 111,
1424
+ "id": "05e9fedd",
1425
+ "metadata": {},
1426
+ "outputs": [
1427
+ {
1428
+ "data": {
1429
+ "text/plain": [
1430
+ "(44413, 10)"
1431
+ ]
1432
+ },
1433
+ "execution_count": 111,
1434
+ "metadata": {},
1435
+ "output_type": "execute_result"
1436
+ }
1437
+ ],
1438
+ "source": [
1439
+ "Cary_new.shape"
1440
+ ]
1441
+ },
1442
+ {
1443
+ "cell_type": "markdown",
1444
+ "id": "6d3ed28a",
1445
+ "metadata": {},
1446
+ "source": [
1447
+ "# Raleigh"
1448
+ ]
1449
+ },
1450
+ {
1451
+ "cell_type": "code",
1452
+ "execution_count": 112,
1453
+ "id": "22b3f101",
1454
+ "metadata": {},
1455
+ "outputs": [],
1456
+ "source": [
1457
+ "import pandas as pd\n",
1458
+ "\n",
1459
+ "Raleigh = pd.read_csv(\"Raleigh.csv\", low_memory=False)"
1460
+ ]
1461
+ },
1462
+ {
1463
+ "cell_type": "markdown",
1464
+ "id": "3633a1d1",
1465
+ "metadata": {},
1466
+ "source": [
1467
+ "## Exploratory Data Analysis"
1468
+ ]
1469
+ },
1470
+ {
1471
+ "cell_type": "code",
1472
+ "execution_count": 114,
1473
+ "id": "6984bfa7",
1474
+ "metadata": {},
1475
+ "outputs": [
1476
+ {
1477
+ "data": {
1478
+ "text/plain": [
1479
+ "Index(['X', 'Y', 'OBJECTID', 'GlobalID', 'case_number', 'crime_category',\n",
1480
+ " 'crime_code', 'crime_description', 'crime_type',\n",
1481
+ " 'reported_block_address', 'city_of_incident', 'city', 'district',\n",
1482
+ " 'reported_date', 'reported_year', 'reported_month', 'reported_day',\n",
1483
+ " 'reported_hour', 'reported_dayofwk', 'latitude', 'longitude', 'agency',\n",
1484
+ " 'updated_date'],\n",
1485
+ " dtype='object')"
1486
+ ]
1487
+ },
1488
+ "execution_count": 114,
1489
+ "metadata": {},
1490
+ "output_type": "execute_result"
1491
+ }
1492
+ ],
1493
+ "source": [
1494
+ "Raleigh.columns"
1495
+ ]
1496
+ },
1497
+ {
1498
+ "cell_type": "code",
1499
+ "execution_count": 140,
1500
+ "id": "10da0e5f",
1501
+ "metadata": {
1502
+ "scrolled": true
1503
+ },
1504
+ "outputs": [
1505
+ {
1506
+ "data": {
1507
+ "text/html": [
1508
+ "<div>\n",
1509
+ "<style scoped>\n",
1510
+ " .dataframe tbody tr th:only-of-type {\n",
1511
+ " vertical-align: middle;\n",
1512
+ " }\n",
1513
+ "\n",
1514
+ " .dataframe tbody tr th {\n",
1515
+ " vertical-align: top;\n",
1516
+ " }\n",
1517
+ "\n",
1518
+ " .dataframe thead th {\n",
1519
+ " text-align: right;\n",
1520
+ " }\n",
1521
+ "</style>\n",
1522
+ "<table border=\"1\" class=\"dataframe\">\n",
1523
+ " <thead>\n",
1524
+ " <tr style=\"text-align: right;\">\n",
1525
+ " <th></th>\n",
1526
+ " <th>year</th>\n",
1527
+ " <th>city</th>\n",
1528
+ " <th>crime_major_category</th>\n",
1529
+ " <th>crime_detail</th>\n",
1530
+ " <th>latitude</th>\n",
1531
+ " <th>longitude</th>\n",
1532
+ " <th>occurance_time</th>\n",
1533
+ " <th>clear_status</th>\n",
1534
+ " <th>incident_address</th>\n",
1535
+ " <th>notes</th>\n",
1536
+ " </tr>\n",
1537
+ " </thead>\n",
1538
+ " <tbody>\n",
1539
+ " <tr>\n",
1540
+ " <th>496313</th>\n",
1541
+ " <td>2024</td>\n",
1542
+ " <td>Raleigh</td>\n",
1543
+ " <td>Miscellaneous</td>\n",
1544
+ " <td>Pornography/Obscene Material</td>\n",
1545
+ " <td>0.0</td>\n",
1546
+ " <td>0.0</td>\n",
1547
+ " <td>2024/01/08 12:18:00</td>\n",
1548
+ " <td>No Data</td>\n",
1549
+ " <td>No Data</td>\n",
1550
+ " <td>District: Northeast</td>\n",
1551
+ " </tr>\n",
1552
+ " <tr>\n",
1553
+ " <th>496314</th>\n",
1554
+ " <td>2024</td>\n",
1555
+ " <td>Raleigh</td>\n",
1556
+ " <td>Miscellaneous</td>\n",
1557
+ " <td>Pornography/Obscene Material</td>\n",
1558
+ " <td>0.0</td>\n",
1559
+ " <td>0.0</td>\n",
1560
+ " <td>2024/01/08 12:18:00</td>\n",
1561
+ " <td>No Data</td>\n",
1562
+ " <td>No Data</td>\n",
1563
+ " <td>District: North</td>\n",
1564
+ " </tr>\n",
1565
+ " <tr>\n",
1566
+ " <th>496315</th>\n",
1567
+ " <td>2024</td>\n",
1568
+ " <td>Raleigh</td>\n",
1569
+ " <td>Miscellaneous</td>\n",
1570
+ " <td>Pornography/Obscene Material</td>\n",
1571
+ " <td>0.0</td>\n",
1572
+ " <td>0.0</td>\n",
1573
+ " <td>2024/01/08 12:19:00</td>\n",
1574
+ " <td>No Data</td>\n",
1575
+ " <td>No Data</td>\n",
1576
+ " <td>District: Southeast</td>\n",
1577
+ " </tr>\n",
1578
+ " <tr>\n",
1579
+ " <th>496316</th>\n",
1580
+ " <td>2024</td>\n",
1581
+ " <td>Raleigh</td>\n",
1582
+ " <td>Sexual Offenses</td>\n",
1583
+ " <td>Sex Offense/Forcible Fondling</td>\n",
1584
+ " <td>0.0</td>\n",
1585
+ " <td>0.0</td>\n",
1586
+ " <td>2024/02/14 21:52:00</td>\n",
1587
+ " <td>No Data</td>\n",
1588
+ " <td>No Data</td>\n",
1589
+ " <td>District: North</td>\n",
1590
+ " </tr>\n",
1591
+ " <tr>\n",
1592
+ " <th>496317</th>\n",
1593
+ " <td>2024</td>\n",
1594
+ " <td>Raleigh</td>\n",
1595
+ " <td>Sexual Offenses</td>\n",
1596
+ " <td>Sex Offense/Statutory Rape</td>\n",
1597
+ " <td>0.0</td>\n",
1598
+ " <td>0.0</td>\n",
1599
+ " <td>2024/02/14 18:19:00</td>\n",
1600
+ " <td>No Data</td>\n",
1601
+ " <td>No Data</td>\n",
1602
+ " <td>District: Northwest</td>\n",
1603
+ " </tr>\n",
1604
+ " </tbody>\n",
1605
+ "</table>\n",
1606
+ "</div>"
1607
+ ],
1608
+ "text/plain": [
1609
+ " year city crime_major_category crime_detail \\\n",
1610
+ "496313 2024 Raleigh Miscellaneous Pornography/Obscene Material \n",
1611
+ "496314 2024 Raleigh Miscellaneous Pornography/Obscene Material \n",
1612
+ "496315 2024 Raleigh Miscellaneous Pornography/Obscene Material \n",
1613
+ "496316 2024 Raleigh Sexual Offenses Sex Offense/Forcible Fondling \n",
1614
+ "496317 2024 Raleigh Sexual Offenses Sex Offense/Statutory Rape \n",
1615
+ "\n",
1616
+ " latitude longitude occurance_time clear_status \\\n",
1617
+ "496313 0.0 0.0 2024/01/08 12:18:00 No Data \n",
1618
+ "496314 0.0 0.0 2024/01/08 12:18:00 No Data \n",
1619
+ "496315 0.0 0.0 2024/01/08 12:19:00 No Data \n",
1620
+ "496316 0.0 0.0 2024/02/14 21:52:00 No Data \n",
1621
+ "496317 0.0 0.0 2024/02/14 18:19:00 No Data \n",
1622
+ "\n",
1623
+ " incident_address notes \n",
1624
+ "496313 No Data District: Northeast \n",
1625
+ "496314 No Data District: North \n",
1626
+ "496315 No Data District: Southeast \n",
1627
+ "496316 No Data District: North \n",
1628
+ "496317 No Data District: Northwest "
1629
+ ]
1630
+ },
1631
+ "execution_count": 140,
1632
+ "metadata": {},
1633
+ "output_type": "execute_result"
1634
+ }
1635
+ ],
1636
+ "source": [
1637
+ "# Define category mapping\n",
1638
+ "category_mapping = {\n",
1639
+ " 'Miscellaneous': ['MISCELLANEOUS', 'ALL OTHER OFFENSES'],\n",
1640
+ " 'Sexual Offenses': ['SEX OFFENSES'],\n",
1641
+ " 'Assault': ['ASSAULT', 'SIMPLE ASSAULT'],\n",
1642
+ " 'Juvenile': ['JUVENILE'],\n",
1643
+ " 'Traffic Violations': ['TRAFFIC', 'UNAUTHORIZED MOTOR VEHICLE USE', 'TRAFFIC VIOLATIONS', 'LIQUOR LAW VIOLATIONS'],\n",
1644
+ " 'Fraud': ['FRAUD', 'EMBEZZLEMENT', 'BRIBERY'],\n",
1645
+ " 'Vandalism': ['VANDALISM'],\n",
1646
+ " 'Theft': ['LARCENY FROM MV', 'LARCENY', 'MV THEFT', 'STOLEN PROPERTY'],\n",
1647
+ " 'Burglary': ['BURGLARY/COMMERCIAL', 'BURGLARY/RESIDENTIAL'],\n",
1648
+ " 'Disorderly Conduct': ['DISORDERLY CONDUCT'],\n",
1649
+ " 'Weapons Violations': ['WEAPONS VIOLATION'],\n",
1650
+ " 'Drugs': ['DRUGS', 'DRUG VIOLATIONS'],\n",
1651
+ " 'Arson': ['ARSON'],\n",
1652
+ " 'Robbery': ['ROBBERY'],\n",
1653
+ " 'Kidnapping': ['KIDNAPPING'],\n",
1654
+ " 'Extortion': ['EXTORTION'],\n",
1655
+ " 'Human Trafficking': ['HUMAN TRAFFICKING'],\n",
1656
+ " 'Murder': ['MURDER'],\n",
1657
+ " 'Prostitution-related Offenses': ['PROSTITUTION'],\n",
1658
+ " 'Gambling': ['GAMBLING'],\n",
1659
+ "}\n",
1660
+ "\n",
1661
+ "# Function to categorize crime based on the mapping dictionary\n",
1662
+ "def categorize_crime(crime):\n",
1663
+ " for category, crimes in category_mapping.items():\n",
1664
+ " if crime in crimes:\n",
1665
+ " return category\n",
1666
+ " return 'Miscellaneous'\n",
1667
+ "\n",
1668
+ "# Create a new DataFrame with simplified crime categories\n",
1669
+ "Raleigh_new = pd.DataFrame({\n",
1670
+ " \"year\": Raleigh['reported_year'],\n",
1671
+ " \"city\": \"Raleigh\",\n",
1672
+ " \"crime_major_category\": Raleigh['crime_category'].apply(categorize_crime),\n",
1673
+ " \"crime_detail\": Raleigh['crime_description'],\n",
1674
+ " \"latitude\": Raleigh['latitude'].round(5).fillna(0),\n",
1675
+ " \"longitude\": Raleigh['longitude'].round(5).fillna(0),\n",
1676
+ " \"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",
1677
+ " \"clear_status\": None,\n",
1678
+ " \"incident_address\": Raleigh['reported_block_address'] + ', ' + Raleigh['district'] + ', Raleigh',\n",
1679
+ " \"notes\": 'District: '+ Raleigh['district'].str.title()\n",
1680
+ "}).fillna(\"No Data\")\n",
1681
+ "\n",
1682
+ "Raleigh_new = Raleigh_new[Raleigh_new['year'] >= 2015]\n",
1683
+ "# Display the last few rows of the new DataFrame\n",
1684
+ "Raleigh_new.tail()\n"
1685
+ ]
1686
+ },
1687
+ {
1688
+ "cell_type": "code",
1689
+ "execution_count": 141,
1690
+ "id": "4561f484",
1691
+ "metadata": {},
1692
+ "outputs": [
1693
+ {
1694
+ "data": {
1695
+ "text/plain": [
1696
+ "(466007, 10)"
1697
+ ]
1698
+ },
1699
+ "execution_count": 141,
1700
+ "metadata": {},
1701
+ "output_type": "execute_result"
1702
+ }
1703
+ ],
1704
+ "source": [
1705
+ "Raleigh_new.shape"
1706
+ ]
1707
+ },
1708
+ {
1709
+ "cell_type": "code",
1710
+ "execution_count": 117,
1711
+ "id": "b36410ab",
1712
+ "metadata": {},
1713
+ "outputs": [
1714
+ {
1715
+ "data": {
1716
+ "text/plain": [
1717
+ "35.99118"
1718
+ ]
1719
+ },
1720
+ "execution_count": 117,
1721
+ "metadata": {},
1722
+ "output_type": "execute_result"
1723
+ }
1724
+ ],
1725
+ "source": [
1726
+ "max(Raleigh_new['latitude'])"
1727
+ ]
1728
+ },
1729
+ {
1730
+ "cell_type": "code",
1731
+ "execution_count": 118,
1732
+ "id": "a4faca3c",
1733
+ "metadata": {},
1734
+ "outputs": [
1735
+ {
1736
+ "data": {
1737
+ "text/plain": [
1738
+ "array([2017, 2016, 2015, 2018, 2019, 2020, 2021, 2022, 2023, 2024])"
1739
+ ]
1740
+ },
1741
+ "execution_count": 118,
1742
+ "metadata": {},
1743
+ "output_type": "execute_result"
1744
+ }
1745
+ ],
1746
+ "source": [
1747
+ "Raleigh_new['year'].unique()"
1748
+ ]
1749
+ },
1750
+ {
1751
+ "cell_type": "markdown",
1752
+ "id": "e0ad795d",
1753
+ "metadata": {},
1754
+ "source": [
1755
+ "# Combined"
1756
+ ]
1757
+ },
1758
+ {
1759
+ "cell_type": "code",
1760
+ "execution_count": 142,
1761
+ "id": "82ed2351",
1762
+ "metadata": {},
1763
+ "outputs": [
1764
+ {
1765
+ "data": {
1766
+ "text/html": [
1767
+ "<div>\n",
1768
+ "<style scoped>\n",
1769
+ " .dataframe tbody tr th:only-of-type {\n",
1770
+ " vertical-align: middle;\n",
1771
+ " }\n",
1772
+ "\n",
1773
+ " .dataframe tbody tr th {\n",
1774
+ " vertical-align: top;\n",
1775
+ " }\n",
1776
+ "\n",
1777
+ " .dataframe thead th {\n",
1778
+ " text-align: right;\n",
1779
+ " }\n",
1780
+ "</style>\n",
1781
+ "<table border=\"1\" class=\"dataframe\">\n",
1782
+ " <thead>\n",
1783
+ " <tr style=\"text-align: right;\">\n",
1784
+ " <th></th>\n",
1785
+ " <th>year</th>\n",
1786
+ " <th>city</th>\n",
1787
+ " <th>crime_major_category</th>\n",
1788
+ " <th>crime_detail</th>\n",
1789
+ " <th>latitude</th>\n",
1790
+ " <th>longitude</th>\n",
1791
+ " <th>occurance_time</th>\n",
1792
+ " <th>clear_status</th>\n",
1793
+ " <th>incident_address</th>\n",
1794
+ " <th>notes</th>\n",
1795
+ " </tr>\n",
1796
+ " </thead>\n",
1797
+ " <tbody>\n",
1798
+ " <tr>\n",
1799
+ " <th>0</th>\n",
1800
+ " <td>2022</td>\n",
1801
+ " <td>Durham</td>\n",
1802
+ " <td>Miscellaneous</td>\n",
1803
+ " <td>Intimidation</td>\n",
1804
+ " <td>36.03734</td>\n",
1805
+ " <td>-78.87843</td>\n",
1806
+ " <td>2022/05/20 12:00:00</td>\n",
1807
+ " <td>Cleared By Arrest</td>\n",
1808
+ " <td>3500 DEARBORN DR</td>\n",
1809
+ " <td>Weapon: (blank)</td>\n",
1810
+ " </tr>\n",
1811
+ " <tr>\n",
1812
+ " <th>1</th>\n",
1813
+ " <td>2019</td>\n",
1814
+ " <td>Durham</td>\n",
1815
+ " <td>Fraud</td>\n",
1816
+ " <td>Fraud-Identity Theft</td>\n",
1817
+ " <td>35.90624</td>\n",
1818
+ " <td>-78.90556</td>\n",
1819
+ " <td>2019/01/01 00:01:00</td>\n",
1820
+ " <td>Inactive</td>\n",
1821
+ " <td>4400 EMERALD FOREST DR</td>\n",
1822
+ " <td>Weapon: (blank)</td>\n",
1823
+ " </tr>\n",
1824
+ " <tr>\n",
1825
+ " <th>2</th>\n",
1826
+ " <td>2020</td>\n",
1827
+ " <td>Durham</td>\n",
1828
+ " <td>Theft</td>\n",
1829
+ " <td>Larceny - Automobile Parts Or Accessories</td>\n",
1830
+ " <td>35.98809</td>\n",
1831
+ " <td>-78.88952</td>\n",
1832
+ " <td>2020/03/09 00:00:00</td>\n",
1833
+ " <td>Active/Open</td>\n",
1834
+ " <td>100 EDGEMONT LN</td>\n",
1835
+ " <td>Weapon: (blank)</td>\n",
1836
+ " </tr>\n",
1837
+ " <tr>\n",
1838
+ " <th>3</th>\n",
1839
+ " <td>2022</td>\n",
1840
+ " <td>Durham</td>\n",
1841
+ " <td>Theft</td>\n",
1842
+ " <td>Towed/Abandoned Vehicle</td>\n",
1843
+ " <td>35.92415</td>\n",
1844
+ " <td>-78.78532</td>\n",
1845
+ " <td>2022/07/19 10:30:00</td>\n",
1846
+ " <td>Closed (Non-Criminal)</td>\n",
1847
+ " <td>1000 ANDREWS CHAPEL RD</td>\n",
1848
+ " <td>Weapon: (blank)</td>\n",
1849
+ " </tr>\n",
1850
+ " <tr>\n",
1851
+ " <th>4</th>\n",
1852
+ " <td>2018</td>\n",
1853
+ " <td>Durham</td>\n",
1854
+ " <td>Drugs</td>\n",
1855
+ " <td>Drug/Narcotic Violations</td>\n",
1856
+ " <td>35.97721</td>\n",
1857
+ " <td>-78.89507</td>\n",
1858
+ " <td>2018/10/18 14:35:00</td>\n",
1859
+ " <td>Inactive</td>\n",
1860
+ " <td>800 DUPREE ST</td>\n",
1861
+ " <td>Weapon: Not Applicable/None</td>\n",
1862
+ " </tr>\n",
1863
+ " </tbody>\n",
1864
+ "</table>\n",
1865
+ "</div>"
1866
+ ],
1867
+ "text/plain": [
1868
+ " year city crime_major_category \\\n",
1869
+ "0 2022 Durham Miscellaneous \n",
1870
+ "1 2019 Durham Fraud \n",
1871
+ "2 2020 Durham Theft \n",
1872
+ "3 2022 Durham Theft \n",
1873
+ "4 2018 Durham Drugs \n",
1874
+ "\n",
1875
+ " crime_detail latitude longitude \\\n",
1876
+ "0 Intimidation 36.03734 -78.87843 \n",
1877
+ "1 Fraud-Identity Theft 35.90624 -78.90556 \n",
1878
+ "2 Larceny - Automobile Parts Or Accessories 35.98809 -78.88952 \n",
1879
+ "3 Towed/Abandoned Vehicle 35.92415 -78.78532 \n",
1880
+ "4 Drug/Narcotic Violations 35.97721 -78.89507 \n",
1881
+ "\n",
1882
+ " occurance_time clear_status incident_address \\\n",
1883
+ "0 2022/05/20 12:00:00 Cleared By Arrest 3500 DEARBORN DR \n",
1884
+ "1 2019/01/01 00:01:00 Inactive 4400 EMERALD FOREST DR \n",
1885
+ "2 2020/03/09 00:00:00 Active/Open 100 EDGEMONT LN \n",
1886
+ "3 2022/07/19 10:30:00 Closed (Non-Criminal) 1000 ANDREWS CHAPEL RD \n",
1887
+ "4 2018/10/18 14:35:00 Inactive 800 DUPREE ST \n",
1888
+ "\n",
1889
+ " notes \n",
1890
+ "0 Weapon: (blank) \n",
1891
+ "1 Weapon: (blank) \n",
1892
+ "2 Weapon: (blank) \n",
1893
+ "3 Weapon: (blank) \n",
1894
+ "4 Weapon: Not Applicable/None "
1895
+ ]
1896
+ },
1897
+ "execution_count": 142,
1898
+ "metadata": {},
1899
+ "output_type": "execute_result"
1900
+ }
1901
+ ],
1902
+ "source": [
1903
+ "NC = pd.concat([Durham_new, Chapel_new, Charlotte_new, Cary_new, Raleigh_new], ignore_index=True)\n",
1904
+ "NC.head()"
1905
+ ]
1906
+ },
1907
+ {
1908
+ "cell_type": "code",
1909
+ "execution_count": 143,
1910
+ "id": "ebd44953",
1911
+ "metadata": {},
1912
+ "outputs": [
1913
+ {
1914
+ "data": {
1915
+ "text/plain": [
1916
+ "(1388438, 10)"
1917
+ ]
1918
+ },
1919
+ "execution_count": 143,
1920
+ "metadata": {},
1921
+ "output_type": "execute_result"
1922
+ }
1923
+ ],
1924
+ "source": [
1925
+ "NC.shape"
1926
+ ]
1927
+ },
1928
+ {
1929
+ "cell_type": "code",
1930
+ "execution_count": 138,
1931
+ "id": "51b553ae",
1932
+ "metadata": {},
1933
+ "outputs": [
1934
+ {
1935
+ "name": "stdout",
1936
+ "output_type": "stream",
1937
+ "text": [
1938
+ "<class 'pandas.core.frame.DataFrame'>\n",
1939
+ "RangeIndex: 1388438 entries, 0 to 1388437\n",
1940
+ "Data columns (total 10 columns):\n",
1941
+ " # Column Non-Null Count Dtype \n",
1942
+ "--- ------ -------------- ----- \n",
1943
+ " 0 year 1388438 non-null int64 \n",
1944
+ " 1 city 1388438 non-null object \n",
1945
+ " 2 crime_major_category 1388438 non-null object \n",
1946
+ " 3 crime_detail 1388438 non-null object \n",
1947
+ " 4 latitude 1388438 non-null float64\n",
1948
+ " 5 longitude 1388438 non-null float64\n",
1949
+ " 6 occurance_time 1388438 non-null object \n",
1950
+ " 7 clear_status 1388438 non-null object \n",
1951
+ " 8 incident_address 1388438 non-null object \n",
1952
+ " 9 notes 1388438 non-null object \n",
1953
+ "dtypes: float64(2), int64(1), object(7)\n",
1954
+ "memory usage: 105.9+ MB\n"
1955
+ ]
1956
+ }
1957
+ ],
1958
+ "source": [
1959
+ "NC.info()"
1960
+ ]
1961
+ },
1962
+ {
1963
+ "cell_type": "code",
1964
+ "execution_count": 144,
1965
+ "id": "5c45ae42",
1966
+ "metadata": {},
1967
+ "outputs": [
1968
+ {
1969
+ "data": {
1970
+ "text/plain": [
1971
+ "array(['Miscellaneous', 'Fraud', 'Theft', 'Drugs', 'Assault',\n",
1972
+ " 'Sexual Offenses', 'Traffic Violations', 'Kidnapping',\n",
1973
+ " 'Weapons Violations', 'Homicide', 'Disorderly Conduct', 'Arson',\n",
1974
+ " 'Animal-related Offenses', 'Prostitution-related Offenses',\n",
1975
+ " 'Gambling', 'Public Service', 'Burglary', 'Missing Person',\n",
1976
+ " 'Robbery', 'Juvenile', 'Vandalism', 'Extortion',\n",
1977
+ " 'Human Trafficking', 'Murder'], dtype=object)"
1978
+ ]
1979
+ },
1980
+ "execution_count": 144,
1981
+ "metadata": {},
1982
+ "output_type": "execute_result"
1983
+ }
1984
+ ],
1985
+ "source": [
1986
+ "NC['crime_major_category'].unique()"
1987
+ ]
1988
+ },
1989
+ {
1990
+ "cell_type": "code",
1991
+ "execution_count": 147,
1992
+ "id": "7fdffffb",
1993
+ "metadata": {},
1994
+ "outputs": [],
1995
+ "source": [
1996
+ "NC.to_csv('NC_dataset.csv', index=False)\n"
1997
+ ]
1998
+ },
1999
+ {
2000
+ "cell_type": "code",
2001
+ "execution_count": 124,
2002
+ "id": "489faa5b",
2003
+ "metadata": {},
2004
+ "outputs": [
2005
+ {
2006
+ "name": "stdout",
2007
+ "output_type": "stream",
2008
+ "text": [
2009
+ "year int64\n",
2010
+ "city object\n",
2011
+ "crime_major_category object\n",
2012
+ "crime_detail object\n",
2013
+ "latitude float64\n",
2014
+ "longitude float64\n",
2015
+ "occurance_time object\n",
2016
+ "clear_status object\n",
2017
+ "incident_address object\n",
2018
+ "notes object\n",
2019
+ "dtype: object\n"
2020
+ ]
2021
+ }
2022
+ ],
2023
+ "source": [
2024
+ "print(NC.dtypes)"
2025
+ ]
2026
+ },
2027
+ {
2028
+ "cell_type": "code",
2029
+ "execution_count": 145,
2030
+ "id": "b4885d64",
2031
+ "metadata": {},
2032
+ "outputs": [
2033
+ {
2034
+ "data": {
2035
+ "text/plain": [
2036
+ "array([2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024])"
2037
+ ]
2038
+ },
2039
+ "execution_count": 145,
2040
+ "metadata": {},
2041
+ "output_type": "execute_result"
2042
+ }
2043
+ ],
2044
+ "source": [
2045
+ "import numpy as np\n",
2046
+ "np.sort(NC['year'].unique())"
2047
+ ]
2048
+ }
2049
+ ],
2050
+ "metadata": {
2051
+ "kernelspec": {
2052
+ "display_name": "Python 3 (ipykernel)",
2053
+ "language": "python",
2054
+ "name": "python3"
2055
+ },
2056
+ "language_info": {
2057
+ "codemirror_mode": {
2058
+ "name": "ipython",
2059
+ "version": 3
2060
+ },
2061
+ "file_extension": ".py",
2062
+ "mimetype": "text/x-python",
2063
+ "name": "python",
2064
+ "nbconvert_exporter": "python",
2065
+ "pygments_lexer": "ipython3",
2066
+ "version": "3.11.5"
2067
+ }
2068
+ },
2069
+ "nbformat": 4,
2070
+ "nbformat_minor": 5
2071
+ }