-
Notifications
You must be signed in to change notification settings - Fork 0
/
Housing_MA.py
491 lines (224 loc) · 7.38 KB
/
Housing_MA.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env python
# coding: utf-8
# # Best Housing Locations in MA
# ## For this project I explore the housing market in MA to determine preferred locations based on housing prices, property tax rate, crime rate, and school performance.
# ### First we'll be looking at property taxes by town for 2019, so we'll need to web scrape a few sites for this data. We'll extract the data into one dataframe and create a choropleth map for visualization purposes.
# In[164]:
import pandas as pd
import requests
url = requests.get('https://www.heislerandmattson.com/2019-massachusetts-tax-rates-real-estate-residential/').text
get_ipython().system('pip install beautifulsoup4')
get_ipython().system('pip install lxml')
from bs4 import BeautifulSoup
taxes = BeautifulSoup(url,'lxml')
print(taxes.prettify())
# In[165]:
p_taxes = taxes.find('table')
p_taxes
# In[166]:
for row in taxes.find_all('tr'):
for col in row.find_all('td'):
print(col.text)
# In[167]:
table_rows = p_taxes.find_all('tr')
res = []
for tr in table_rows:
td = tr.find_all('td')
row = [tr.text.strip() for tr in td if tr.text.strip()]
if row:
res.append(row)
# In[168]:
prop_taxes = pd.DataFrame(res, columns=["Town", "Year", "Property_Taxes", "blah"])
prop_taxes.head()
# In[169]:
del prop_taxes['Year']
del prop_taxes['blah']
prop_taxes.head()
# In[170]:
prop_taxes.drop(prop_taxes.index[0], inplace=True)
prop_taxes.head()
# In[171]:
zip_url = requests.get('https://www.zip-codes.com/state/ma.asp').text
zip = BeautifulSoup(zip_url,'lxml')
print(zip.prettify())
# In[172]:
zips = zip.find('table',{'class':'statTable'})
zips
# In[173]:
for row in zips.find_all('tr'):
for col in row.find_all('td'):
print(col.text)
# In[174]:
table_rows2 = zips.find_all('tr')
res = []
for tr in table_rows2:
td = tr.find_all('td')
row = [tr.text.strip() for tr in td if tr.text.strip()]
if row:
res.append(row)
# In[175]:
zip_codes = pd.DataFrame(res, columns=["Zip", "Town", "County", "Type"])
zip_codes.head()
# In[176]:
del zip_codes['Type']
del zip_codes['County']
zip_codes.head(10)
# In[177]:
zip_codes.drop(zip_codes.index[0], inplace=True)
zip_codes.head()
# In[178]:
zip_codes['Zip'] = zip_codes['Zip'].str.strip('ZIP Code ')
zip_codes.head(10)
# ### Someone was kind enough to create a .csv list of zip codes with coordinates on github. I use this to get the latitude and longitudes for each zip code
# In[179]:
coords=pd.read_csv('https://gist.githubusercontent.com/erichurst/7882666/raw/5bdc46db47d9515269ab12ed6fb2850377fd869e/US%2520Zip%2520Codes%2520from%25202013%2520Government%2520Data', dtype={'ZIP': str})
coords.head(5)
# ### Rename 'ZIP' to 'Zip' for consistency between dataframes for merging
# In[180]:
coords = coords.rename(columns={"ZIP": "Zip"})
coords.head(5)
# In[181]:
coords.dtypes
# In[182]:
prop_taxes.dtypes
# In[183]:
zip_codes.dtypes
# ### Merge the first 2 dataframes on the shared 'Town' column.
# In[184]:
MA1 = pd.merge(zip_codes, prop_taxes, on='Town', how='outer')
MA1.head()
# In[185]:
MA1.shape
# ### Now, merge that dataframe with the dataframe for coordinates on the shared 'Zip' column
# In[186]:
Taxes = pd.merge(MA1, coords, on='Zip', how='outer')
Taxes.head()
# In[187]:
Taxes.shape
# In[188]:
Taxes['Property_Taxes'] = pd.to_numeric(Taxes.Property_Taxes.astype(str).str.replace(',',''), errors='coerce')
#.fillna(0)
#.astype(int)
Taxes.head()
# In[189]:
del Taxes['Zip']
Taxes.head()
# In[190]:
Taxes.drop_duplicates(['Town'], keep='first', inplace=True)
Taxes.head(20)
# In[191]:
Taxes.isnull().sum()
# In[192]:
Taxes.shape
# In[193]:
pd.set_option('max_rows', 500)
Taxes
# In[194]:
Taxes = Taxes.rename({'Town': 'TOWN'}, axis=1)
Taxes.head()
# In[59]:
get_ipython().system('conda install -c conda-forge folium=0.5.0 --yes')
import folium
print('Folium installed and imported!')
# In[29]:
# Massachusetts latitude and longitude values
latitude = 42.40
longitude = -71.38
# In[60]:
# create map and display it
MA_map = folium.Map(location=[latitude, longitude], zoom_start=8)
# display the map of MA
MA_map
# In[61]:
for i in range(0,len(Taxes)):
folium.Marker([Taxes.iloc[i]['LAT'], Taxes.iloc[i]['LNG']], popup=Taxes.iloc[i]['Property_Taxes']).add_to(MA_map)
MA_map
# In[62]:
conda install -c conda-forge geopandas
# In[63]:
conda install -c conda-forge/label/gcc7 descartes
# In[64]:
from matplotlib import pyplot
from shapely.geometry import LineString
from descartes import PolygonPatch
import geopandas as gpd
# In[195]:
# set the filepath and load in a shapefile
MA_map = gpd.read_file(r'C:\Users\Nick\Desktop\GitProjects\Housing_MA\townssurvey_shp\TOWNSSURVEY_POLYM.shp')
# check data type so we can see that this is not a normal dataframe, but a GEOdataframe
MA_map.head()
# In[196]:
MA_map.plot()
# In[197]:
Taxes['TOWN'] = Taxes['TOWN'].str.upper()
Taxes.head()
# In[354]:
Taxes.shape
# In[199]:
MA_map.shape
# In[200]:
MA_tax = pd.merge(MA_map, Taxes, on='TOWN', how='outer')
MA_tax.head()
# In[271]:
MA_tax.shape
# In[272]:
MA_tax = MA_tax[['TOWN','Property_Taxes','LAT','LNG','TOWN_ID','geometry']]
MA_tax.head()
# In[273]:
MA_tax.loc[MA_tax['Property_Taxes'].isnull()]
# In[355]:
MA_tax.shape
# In[356]:
MA_Tax.describe()
# In[357]:
MA_Tax.isnull().sum()
# In[211]:
MA_tax.dropna(subset = ['geometry'], inplace=True)
MA_tax.isnull().sum()
# In[358]:
MASS_taxes = gpd.GeoDataFrame(MA_tax, geometry='geometry', crs={'init': 'epsg:4326'})
MASS_taxes.head()
# In[359]:
MASS_taxes.plot()
# In[360]:
import json
#Read data to json.
merged_json = json.loads(MASS_taxes.to_json())
#Convert to String like object.
MASS_Taxes = json.dumps(merged_json)
# In[361]:
MASS_taxes.crs
# In[98]:
conda install bokeh
# In[99]:
from bokeh.io import output_notebook, show, output_file
from bokeh.plotting import figure
from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar
from bokeh.palettes import brewer
# In[363]:
geosource = GeoJSONDataSource(geojson = MASS_Taxes)
#set the color palette
palette = brewer['YlOrRd'][8]
palette = palette[::-1]
color_mapper = LinearColorMapper(palette = palette, low = 0, high = 25, nan_color = 'Black')
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 500, height = 20,
border_line_color='black',location = (0,0), orientation ='horizontal', major_label_overrides = tick_labels)
#Set the size and title of the graph
p = figure(title = 'Massachusetts Residential Property Taxes for 2019', plot_height = 700 , plot_width = 1020, toolbar_location = 'below',
toolbar_sticky=False,tooltips=[
("Town", "@TOWN"),
("Property Tax (per $1,000)","@Property_Taxes{$1.11}")])
#Define custom tick labels for color bar.
tick_labels = {'0': '$0/Unknown', '5':'$5', '10':'$10', '15':'$15', '20':'$20', '25':'$25'}
#Drop the Axes to clean it up
p.axis.visible = False
#Makes it so there are no gird lines
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.patches('xs','ys', source = geosource,fill_color = {'field':'Property_Taxes', 'transform' : color_mapper},
line_color = 'black', line_width = 0.75, fill_alpha = 2)
p.add_layout(color_bar, 'above')
output_file(r"C:\Users\Nick\Desktop\GitProjects\Housing_MA\Property_Tax_Choropleth_Map.html")
output_notebook()
show(p)
# In[ ]: