Unlocking Hidden Patterns

Learn the power of data visualization in revealing insights.

Now, it’s time to reap the fruits of our hard work. Let’s visualize the cluster results on a map to see if we can identify any patterns. These patterns could be beneficial for the salesperson to determine the route.

Creating interactive maps with folium

The folium python library can be used to create interactive maps with layers and markers. The ability to use HTML web pages and visualizations as markers on the map is one of the greatest strengths of folium and opens up endless possibilities for making the maps more interactive and informative. Therefore, it’s a useful addition to plotly.

First, we plot the first five stores using folium:

Press + to interact
import pandas as pd
import os
import folium
df = pd.read_csv("StoreCluster.csv")
# Add a 'Store' column
storeMaster = pd.read_csv('MoscowMcD.csv')
name = storeMaster['Store']
storeMaster['Store'] = storeMaster.index+1
# Merge the two df based on the 'Store' column
storeClusterCoord = pd.merge(storeMaster, df, on='Store')
lon = storeClusterCoord['lon']
lat = storeClusterCoord['lat']
cluster = storeClusterCoord['Segment']
m = folium.Map(location=[lat[0], lon[0]], zoom_start=10)
folium.Marker(
location=[lat[0], lon[0]],
popup=name[0],
icon=folium.Icon(icon="cloud"),
).add_to(m)
folium.Marker(
location=[lat[1], lon[1]],
popup=name[1],
icon=folium.Icon(color="green"),
).add_to(m)
folium.Marker(
location=[lat[2], lon[2]],
popup=name[2],
icon=folium.Icon(color="red", icon="info-sign"),
).add_to(m)
folium.Marker(
location=[lat[3], lon[3]],
popup=name[3],
icon=folium.Icon(color="red", icon="info-sign"),
).add_to(m)
folium.Marker(
location=[lat[4], lon[4]],
popup=name[4],
icon=folium.Icon(color="red", icon="info-sign"),
).add_to(m)
m

Loop with iterrows

This was not a challenge; after all, we already have a lot of experience with creating maps. Let’s repeat the previous step for all stores now. We can manually add the seven missing stores as code blocks, as we did in the example above. Or we can save ourselves the trouble and instead ...