Home > Net >  How do assign color to Folium Map based on another Column value?
How do assign color to Folium Map based on another Column value?

Time:01-06

I have the following data structure (700 rows, 6 clusters), an example is shown below.

uID LAT         LNG         cluster_label
1   29.488112   -12.144520  1
3   11.143642   -30.761047  0
10  21.230476   -25.757441  1
16  61.196442   -23.394233  1

I want to graph it on a map using folium which I have done successfully. However, I am struggling to figure out how to change the marker color based on the cluster_label column. Below is my map code.

#Create map
m = folium.Map(location=[40, -90], zoom_start=4)

#Create loop to add marker one by one
for i in range(0,len(df4)):
   folium.Marker(
      location=[df4.iloc[i]['LAT'], df4.iloc[i]['LNG']],
      popup = df4.iloc[i]['uID'],
   ).add_to(m)

#Show map
m

I've tried adding

color = df4['cluster_label']

to the code but am getting "TypeError: Object of type Series is not JSON serializable".

I've also tried defining a function with if statements assigning color based on the cluster_label value and then add getcolor to the code (based on another SO post) but that did not work.

Any help is greatly appreciated.

CodePudding user response:

Sample data is used from the enter image description here

  • Related