Created
February 14, 2022 13:46
-
-
Save pplonski/85d4ee6ef42b42776889a19235c1dbda to your computer and use it in GitHub Desktop.
SF Crimes Dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Dashboard of San Fransisco Crime Dataset with Mercury" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import folium\n", | |
"from folium import plugins\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df = pd.read_csv(\"./data/SF_crime.csv\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# those variables will be updated by Mercury framework\n", | |
"# please keep all variables to be updated in one cell\n", | |
"limit = 1000\n", | |
"district = ['BAYVIEW', 'NORTHERN']\n", | |
"category = ['VANDALISM', 'ASSAULT', 'ROBBERY']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# the code for plotting is based on https://github.com/marcellusruben/sf-crime-voila/blob/main/sf_crime.ipynb\n", | |
"df = df.iloc[0:limit, :]\n", | |
"\n", | |
"latitude = 37.77\n", | |
"longitude = -122.42\n", | |
"\n", | |
"df_dist = df.loc[df['PdDistrict'].isin(np.array(district))]\n", | |
"df_category = df_dist.loc[df_dist['Category'].isin(np.array(category))]\n", | |
"\n", | |
"cat_unique = df_category['Category'].value_counts()\n", | |
"cat_unique = cat_unique.reset_index()\n", | |
"\n", | |
"dist_unique = df_category['PdDistrict'].value_counts()\n", | |
"dist_unique = dist_unique.reset_index()\n", | |
"\n", | |
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))\n", | |
"\n", | |
"# create map and display it\n", | |
"sanfran_map = folium.Map(location=[latitude, longitude], zoom_start=12)\n", | |
"\n", | |
"\n", | |
"incidents = plugins.MarkerCluster().add_to(sanfran_map)\n", | |
"\n", | |
"# loop through the dataframe and add each data point to the mark cluster\n", | |
"for lat, lng, label, in zip(df_category.Y, df_category.X, df_category.Category):\n", | |
" folium.Marker(\n", | |
" location=[lat, lng],\n", | |
" icon=None,\n", | |
" popup=label,\n", | |
" ).add_to(incidents)\n", | |
"# show map\n", | |
"display(sanfran_map)\n", | |
"\n", | |
"ax1.bar(cat_unique['index'], cat_unique['Category'])\n", | |
"ax1.set_title('Amount of Criminal Case Based on Category')\n", | |
"ax2.bar(dist_unique['index'], dist_unique['PdDistrict'])\n", | |
"ax2.set_title('Amount of Criminal Case in Selected District')\n", | |
"\n", | |
"plt.show()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "dashenv", | |
"language": "python", | |
"name": "dashenv" | |
}, | |
"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.8.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment