In [10]:
from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)

# This line will hide code by default when the notebook is exported as HTML
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)

# This line will add a button to toggle visibility of code blocks, for use with the HTML export version
di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True)
In [11]:
import warnings

Interactive Heat Map of Rat Sightings in NYC

This is the heat map described in my Medium article on NYC's rat issue:

To check out your neighborhood, simply use the + / - buttons to zoom in and out and use your mouse to move the map by clicking and dragging.

In [1]:
#importing packages
%matplotlib inline
import scipy.stats as stats
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import folium
from folium import plugins'seaborn')
In [2]:
#importing data set
df = pd.read_csv('Rat_Sightings.csv')
In [3]:
#removing all rows with NaN's in Longitude or Latitude column
df = df[np.isfinite(df['Latitude'])]
In [4]:
df = df[np.isfinite(df['Longitude'])]
In [6]:
#converting date column to datetime format
df['Created Date'] = pd.to_datetime(df['Created Date'])
In [7]:
#splitting the data up into separate years
tten = df[df['Created Date'].dt.year == 2010]
televen = df[df['Created Date'].dt.year == 2011]
ttwelve = df[df['Created Date'].dt.year == 2012]
tthirteen = df[df['Created Date'].dt.year == 2013]
tfourteen = df[df['Created Date'].dt.year == 2014]
tfifteen = df[df['Created Date'].dt.year == 2015]
tsixteen = df[df['Created Date'].dt.year == 2016]
tseventeen = df[df['Created Date'].dt.year == 2017]
In [12]:
#generating folium map
m = folium.Map([40.7, -73.9], zoom_start=11)
In [13]:
# convert to (n, 2) nd-array format for heatmap
rats = tseventeen[['Latitude', 'Longitude']].as_matrix()

# plot heatmap
m.add_children(plugins.HeatMap(rats, radius=15))
In [ ]: