Mapping Data Using Folium

Gregory Han
3 min readJan 18, 2022

--

Sometimes, you run across a dataset that contain locations such as zip codes or even longitudes and latitudes. After cleaning the data and analyzing it, you realize you have some questions:

  • Where do these locations lie on a map?
  • What trends can be seen when mapped?

One method of visualizing the data to answer these questions is to make folium maps in Python. Here is a quick tutorial on creating a colorful folium map using our dataset.

Importing a sample dataset

The sample dataset that we will use to create folium map is the King County Housing Data available on kaggle.com as a .csv file. We will then make a folium map to show homes, and their prices in King County, WA (Seattle, Washington area).

https://www.kaggle.com/harlfoxem/housesalesprediction

We will import the csv using the Pandas Dataframe.

import pandas as pd
df = pd.read_csv('kc_house_data.csv')

Taking a look at the columns:

df.columnsIndex(['id', 'date', 'price', 'bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'waterfront', 'view', 'condition', 'grade', 'sqft_above', 'sqft_basement' ,'yr_built', 'yr_renovated', 'zipcode', 'lat', 'long', 'sqft_living15', 'sqft_lot15', 'grade_value', 'age', 'condition_num'],dtype='object')

we see that there is a lat for latitude and long for longitude.

Setting up parameters to map

Before making a folium map, we can also do exploratory data analysis to decide what we want to visualize. Let’s say, for our analysis, we want to map only homes with 3 bedrooms.

df3 = df[df['bedrooms'] == 3]

Making a folium map

Let’s import the folium package and set up where the center of the map will be. In this example, we will use the median of the lat, long columns in our dataset as the center.

import folium#creating the map center
m = folium.Map(location=[47.560093, -122.213982], zoom_start=10)

We create a for loop to iterate through the dataset to make a circle point for each home and then plot that circle point on our map.

for i in range(len(df3)):    
folium.Circle(
location=[df3.iloc[i]['lat'], df3.iloc[i]['long']],
radius=10,
).add_to(m)
image from ‘visualizing housing data with folium maps’ by aaron lee

However, because the points are all the same color, it is hard to make any visual analysis from this map.

Coloring our world

We can add color to our map to make it more useful using the colormap package. We input the colors [‘blue’, ‘green’, ‘yellow’] to create a gradient where blue will be the lower priced homes to yellow which are the higher priced homes. Let’s set the vmin (minimum of range) as $0 and vmax (maximum of range) as $730,000 to only plot and color points that contain homes in our range.

colormap = cm.LinearColormap(colors=['blue', 'green', 'yellow'], vmin=0, vmax=730000)
m = folium.Map(location=[47.560093, -122.213982], zoom_start=10)
for
i in range(len(df3)):
folium.Circle(
location=[df3.iloc[i]['lat'], df3.iloc[i]['long']],
radius=10,
fill=True,
color=colormap(df3.iloc[i]['price']),
fill_opacity=0.2
).add_to(m)
m.add_child(colormap)

Awesome! Now just from looking at our map, we can see that homes that have 3 bedrooms are more expensive closer to the Seattle metropolitan area and cheaper the further away it is.

Leveling up with folium

There are more customization techniques to create a variety of different folium maps. For example, with feature engineering you can even create a map to show homes that are on the waterfront:

image from ‘visualizing housing data with folium maps’ by aaron lee

or use choropleth maps to show mean home prices by zip code:

image from ‘visualizing housing data with folium maps’ by aaron lee

Have fun making beautiful and creative folium maps!

--

--

Gregory Han

Aspiring data scientist and lover of dogs, especially greyhounds.