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
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Omaha Incidents"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data Exploration\n",
"\n",
"Let\"s explore the data a little bit to see what kind of analysis and visualizations we want to implement."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip3 install ipykernel\n",
"!pip3 install --upgrade pandas plotly dash \"nbformat>=4.2.0\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import sqlite3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"connection = sqlite3.connect(\"../raw_data/ingress.db\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cursor = connection.cursor()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Test query to see if the data loaded\n",
"select_all = \"SELECT * FROM incidents;\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_sql_query(select_all, connection)\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# test plotting by sorting & plotting top 5 crime categories\n",
"s = df.value_counts(subset=[\"description\"])\n",
"t = s.nlargest(5)\n",
"t.head()\n",
"t.plot(kind=\"bar\", title=\"Top 5 Incident Categories\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.express as px\n",
"import plotly.graph_objects as go"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s.head(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filtered_df = df[(df['date'] > '01/01/2023') & (df['date'] < '12/31/2023')]\n",
"filtered_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = px.scatter_mapbox(\n",
" filtered_df,\n",
" lat=\"lat\",\n",
" lon=\"lon\",\n",
" color=\"description\",\n",
" hover_name=\"description\",\n",
" hover_data=[\"date\", \"time\"],\n",
" title=\"Incident Count by Coordinates\",\n",
" center={\"lat\": 41.257160, \"lon\": -95.995102},\n",
" zoom=10\n",
")\n",
"\n",
"# fig.update_layout(showlegend=False)\n",
"fig.update_layout(mapbox_style=\"open-street-map\")\n",
"fig.update_layout(margin={\"r\": 0, \"t\": 0, \"l\": 0, \"b\": 0})\n",
"fig.update_layout(mapbox_bounds={\"west\": -180, \"east\": -50, \"south\": 20, \"north\": 90})\n",
"# fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.io as pio\n",
"pio.write_html(fig, file=\"test.html\", auto_open=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# clean up and close it out\n",
"connection.commit()\n",
"connection.close()"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|