Visualizing the data

Since this data is based on ZIP codes, the best way to visualize it is with a choropleth. If you're unfamiliar with a choropleth, it's simply a visualization that represents the data according to a color spectrum. Let's create one now using a Python mapping library called folium at https://github.com/python-visualization/folium. If you don't have folium installed, again, it can be done with pip install on the command line.

Now we'll go ahead and create our visualization:

import folium 
 
m = folium.Map(location=[40.748817, -73.985428], zoom_start=13) 
 
m.choropleth( 
    geo_data=open('nyc.json').read(), 
    data=zdf_mean, 
    columns=['zip', 'avg_rent'], 
    key_on='feature.properties.postalCode', 
    fill_color='YlOrRd', fill_opacity=0.7, line_opacity=0.2, 
    ) 
 
m 

There's a lot going on here, so let's take it step by step:

  1. After importing folium, we create a .Map() object. We need to pass in coordinates and a zoom level to center the map. A Google search for the coordinates of the Empire State Building will give us the proper lat and long (flip the sign on the longitude to render it properly). Finally, adjust the zoom to get it centered appropriately for our data.
  2. The next line requires something called a GeoJSON file. This is an open format for representing geographic attributes. This can be found by searching for NYC GeoJSON files—specifically, ones with ZIP code mappings. Once that is done, we reference the GeoJSON file by inputting its path.
  3. Next, we reference our DataFrame in the data parameter. Here, we are using the mean rent by ZIP code we created previously. The columns parameter references those. The key_on parameter references the part of our JSON file that we are targeting, in this instance, the postalCode.
  4. Finally, the other options determine the color palette and certain other parameters to adjust the legend and coloring.

When the cell is run, the map should render inline in your Jupyter Notebook, as can be seen in the following diagram:

With the heat map completed, you can begin to get a sense of which areas have higher or lower rents. This could help when targeting a particular area, but let's take our analysis deeper by using regression modeling.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.82.217