Skip to content

Instantly share code, notes, and snippets.

@erikgregorywebb
Created November 30, 2018 03:58
Show Gist options
  • Save erikgregorywebb/8d4ebf356b43cf1d279aea3aeae1e3f7 to your computer and use it in GitHub Desktop.
Save erikgregorywebb/8d4ebf356b43cf1d279aea3aeae1e3f7 to your computer and use it in GitHub Desktop.
# import libraries
import pandas as pd
import numpy as np
import folium
import matplotlib.cm
# define color vector function
def ColorVector(values, number_of_colors, cmap_name):
buckets = pd.qcut(values, number_of_colors).codes
cmap = matplotlib.cm.get_cmap(name = cmap_name)
colors = []
for i in range(0, number_of_colors):
r = int(cmap(i/number_of_colors)[0]*255)
g = int(cmap(i/number_of_colors)[1]*255)
b = int(cmap(i/number_of_colors)[2]*255)
color = "#{0:02x}{1:02x}{2:02x}".format(r, g, b)
colors.append(color)
values = []
for j in range(0, len(buckets)):
value = colors[buckets[j]]
values.append(value)
return values
# define color categories function
def ColorCategories(variable, colors):
properties['Colors'] = colors
counts = properties.groupby(['Colors', variable]).size().reset_index().rename(columns={0:'count'})
color_list = counts.Colors.unique()
for color in color_list:
temp = counts[(counts.Colors == color)]
print(color, min(temp[variable]), max(temp[variable]), len(temp))
# read in the data
properties = pd.read_csv('/Users/erikgregorywebb/Documents/Python/scarsdale/data/scarsdale-properties-final.csv')
# prepare map components
lats = properties['LAT']
lngs = properties['LNG']
vals = properties['TotalAssessedVal']
addresses = properties['Address']
sqft = properties['SqFootLivingArea']
owners = properties['Owner']
##### View 1 - Year Built #####
years = []
for year in year_built:
if pd.isnull(year) == False:
year = int(year)
else:
year = None
years.append(year)
# coloring
properties['YearBuilt'] = years
colors = ColorVector(properties['YearBuilt'].values, 20, 'coolwarm')
ColorCategories('YearBuilt', colors)
# determine color vector
colors = ColorVector(properties['YearBuilt'].values, 20, 'coolwarm')
# create base map
m = folium.Map(location=[np.mean(properties.LAT), np.mean(properties.LNG)],
tiles = 'Stamen Toner', zoom_start = 13)
# loop through properties to add map layers
for i in range(len(lats)):
folium.Circle(location=[lats[i], lngs[i]],
popup = ("""<b>{}</b><br>Year Built: {}""".format(addresses[i], years[i])),
radius = 20,
color = colors[i],
fill = True,
fill_color = colors[i],
fill_opacity=.30).add_to(m)
# save map as html file
m.save('view-1.html')
##### View 2 - Total Assessed Value #####
# stylize as currency
dollar_vals = []
for i in range(0, len(vals)):
dollar_val = '${:0,.0f}'.format(vals[i]).replace('$-','-$')
# stylize as number (commas)
num_sqfts = []
for i in range(0, len(sqft)):
num_sqft = '{:0,.0f}'.format(sqft[i])
num_sqfts.append(num_sqft)
# determine color vector
colors = ColorVector(properties['TotalAssessedVal'].values, 10, 'plasma')
ColorCategories('TotalAssessedVal', colors)
# create the base map
m = folium.Map(location=[np.mean(properties.LAT), np.mean(properties.LNG)],
tiles = 'Stamen Toner', zoom_start = 13)
# loop through properties to add map layers
for i in range(len(lats)):
folium.Circle(location=[lats[i], lngs[i]],
popup = ("""<b>{}</b><br>{}<br>{} sq. ft.<br>""".format(addresses[i], dollar_vals[i], num_sqfts[i])),
radius = 20,
color = colors[i],
fill = True,
fill_color = colors[i],
fill_opacity=.30).add_to(m)
# save map as html file
m.save('view-2.html')
##### View 3 - Years Since Last Sale #####
# calculate the number of years since last sale
SalesDates = list(properties['SaleDate'])
YearsSinceSale = []
for SaleDate in SalesDates:
if pd.isnull(SaleDate) == False:
Year = int(SaleDate[-2:])
if Year < 19:
Year = Year + 2000
else:
Year = Year + 1900
Years = 2018 - Year
else:
Year = None
Years = None
YearsSinceSale.append(Years)
properties['YearsSinceSale'] = YearsSinceSale
colors = ColorVector(properties['YearsSinceSale'].values, 15, 'summer')
ColorCategories('YearsSinceSale', colors)
# replace Nan with 'None' for simplicity
properties['SaleDate'] = properties['SaleDate'].fillna('None')
SalesDates = list(properties['SaleDate'])
# create the base map
m = folium.Map(location=[np.mean(properties.LAT), np.mean(properties.LNG)],
tiles = 'Stamen Toner', zoom_start = 13)
# loop through properties to add map layers
for i in range(len(lats)):
folium.Circle(location=[lats[i], lngs[i]],
popup = ("""<b>{}</b><br>Sale Date: {}<br>Years Since Sale: {}""".format(addresses[i], SalesDates[i], YearsSinceSale[i])),
radius = 15,
color = colors[i],
fill = True,
fill_color = colors[i],
fill_opacity=.7).add_to(m)
# save map as html file
m.save('view-3.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment