-
Introduction 1
-
Lecture1.1
-
-
Getting the Data 4
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
-
Location Groups 3
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
-
Creating the New Data 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Mapping the Data 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
Finishing the Map
Solution
def Create_Map():
gmap = gmplot.GoogleMapPlotter.from_geocode("Boston")
for index, row in MapData.iterrows():
xPoints=[row["Lat 2"],row["Lat 0"],row["Lat 0"],row["Lat 2"]]
yPoints=[row["Lon 0"],row["Lon 0"],row["Lon 2"],row["Lon 2"]]
gmap.polygon(xPoints, yPoints, color="#ff0000")
gmap.draw("Boston_Real_Estate.html")
Create_Map()
We use iterrows to go through each row of our data. From these rows we can get the latitude and longitude, for plotting.
Now, what if we wanted to draw the squares as red if the average value is low, and green if the value is high? We could get percentile ranks for each location code, and then plot the colors based on percentile.
MapData['Rank'] = MapData["AV_TOTAL"].rank(pct=True)
def Create_Map():
gmap = gmplot.GoogleMapPlotter.from_geocode("Boston")
for index, row in MapData.iterrows():
xPoints=[row["Lat 2"],row["Lat 0"],row["Lat 0"],row["Lat 2"]]
yPoints=[row["Lon 0"],row["Lon 0"],row["Lon 2"],row["Lon 2"]]
if row["Rank"]<=.5:
color ='#%02X%02X%02X' % (255,0,0)
else:
color ='#%02X%02X%02X' % (0,255,0)
gmap.polygon(xPoints, yPoints, color=color)
gmap.draw("Boston_Real_Estate.html")
Create_Map()
The line MapData[‘Rank’] = MapData[“AV_TOTAL”].rank(pct=True) gives us a column representing the percentile rank of each location group.
Something else we could do is make the really poor locations even more red, and the really rich ones even more green through the code below.
def Create_Map():
gmap = gmplot.GoogleMapPlotter.from_geocode("Boston")
for index, row in MapData.iterrows():
xPoints=[row["Lat 2"],row["Lat 0"],row["Lat 0"],row["Lat 2"]]
yPoints=[row["Lon 0"],row["Lon 0"],row["Lon 2"],row["Lon 2"]]
if row["Rank"]<=.5:
color ='#%02X%02X%02X' % (int(255*((1-row["Rank"]*2))),0,0)
else:
color ='#%02X%02X%02X' % (0,int(255*((row["Rank"]-.5)*2)),0)
gmap.polygon(xPoints, yPoints, color=color)
gmap.draw("Boston_Real_Estate.html")
Create_Map()
Now, for the final touch, let’s find growth rates for each location and plot them.
MapData["Growth"] = MapData["AV_TOTAL"]/MapData["AV_TOTAL_PRE"]
MapData['Rank'] = MapData["Growth"].rank(pct=True)
MapData = MapData[MapData["Growth"].notnull()]
Create_Map()
Source Code