Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Hirosaji/a12104fc6f1abb8f77a40208d45dbb41 to your computer and use it in GitHub Desktop.
Save Hirosaji/a12104fc6f1abb8f77a40208d45dbb41 to your computer and use it in GitHub Desktop.
Visualize dataset includes crimes reported to the New York City Police Department.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install pandas\n",
"!pip install keplergl\n",
"!pip install geopandas\n",
"!pip install geofeather\n",
"!pip install pygeos"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"from keplergl import KeplerGl\n",
"import geopandas as gpd\n",
"\n",
"# Read Crimes data\n",
"# https://www.kaggle.com/mihalw28/nypd-complaint-data-current-ytd-july-2018\n",
"df = gpd.read_file('./data/NYPD_Complaint_Data_Current_YTD.csv', encoding='utf-8')\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"#Create a basemap \n",
"map = KeplerGl(height=600, width=800)\n",
"\n",
"#show the map\n",
"map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Convert to float\n",
"df.Latitude = pd.to_numeric(df.Latitude, downcast='float')\n",
"df.Longitude = pd.to_numeric(df.Longitude, downcast='float')\n",
"\n",
"# Create a gepdataframe\n",
"gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude), crs='EPSG:4326')\n",
"\n",
"# Add data to Kepler\n",
"map.add_data(data=gdf, name='crimes')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read NYC polygon shapefile\n",
"# https://data.cityofnewyork.us/City-Government/Neighborhood-Tabulation-Areas-NTA-/cpf4-rkhq\n",
"polygon_shp = gpd.read_file('./data/ny_boundaries/Neighborhood Tabulation Areas (NTA).shp', encoding='SHIFT-JIS')\n",
"polygon_shp.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Convert Shapefile to geofeather\n",
"import geofeather\n",
"\n",
"geofeather.to_geofeather(polygon_shp, 'data/ny_boundaries/nycities.feather')\n",
"polygon_feather = geofeather.from_geofeather('data/ny_boundaries/nycities.feather')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def count_incidents_neighborhood(data, neighb):\n",
" # spatial join and group by to get count of incidents in each poneighbourhood \n",
" joined = gpd.sjoin(data, neighb, op='within')\n",
" grouped = joined.groupby('ntacode').size()\n",
" df = grouped.to_frame().reset_index()\n",
" df.columns = ['ntacode', 'count']\n",
" merged = neighb.merge(df, on='ntacode', how='outer')\n",
" merged['count'].fillna(0,inplace=True)\n",
" merged['count'] = merged['count'].astype(int)\n",
" return merged"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"merged_gdf = count_incidents_neighborhood(gdf, polygon_feather)\n",
"\n",
"map2 = KeplerGl(height=600, width=800)\n",
"\n",
"# Add data to Kepler\n",
"map2.add_data(data=merged_gdf, name='NeighborhoodCrimes')\n",
"map2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment