I would like to customize one of my variables by styling it with a different color.
I tried to make the IF statement, but unfortunately, it doesn't work as expected.
df = pd.read_csv("or_geo.csv")
fo=folium.FeatureGroup(name="OR",overlay = True)
for i,row in df.iterrows():
lat =df.at[i, 'lat']
lng = df.at[i, 'lng']
sp = df.at[i, 'sp']
stat = df.at[i,'status']
popup = df.at[i,'sp'] +'<br>' + str(df.at[i, 'street']) + '<br>' + str(df.at[i, 'post code']) + '<br>' + stat
if stat =="To be surveyed":
'<strong style="text-transform:uppercase; color:red;">' + str(df.at[i,'status']) +'</strong>'
elif stat =="Survey arranged":
'<strong style="text-transform:uppercase; color:blue;">' + str(df.at[i,'status']) +'</strong>'
else:
'<strong style="text-transform:uppercase; color:green;">' + str(df.at[i,'status']) +'</strong>'
fo.add_child(folium.Marker(location=[lat,lng], popup=popup, icon =
folium.Icon(color='blue', icon='glyphicon-calendar')))
map.add_child(fo)
I have no errors, but I have no results either, as my font still comes default
The data looks like this:
UPDATE:
After applying
popup = df.at[i,'sp'] +'<br>' + str(df.at[i, 'street']) + '<br>' + str(df.at[i, 'post code']) + '<br>' .format(row[stat], style)
I've noticed, that Python throws the Key error referring to the very first record in the 'status' row in my .csv file.
I am getting error: Key error: 'To be surveyed'
which corresponds to the value in my row
This is achieved by adding the variable of the result of the decision to the popup at the beginning of the loop process instead of making a conditional decision in the folium settings. Since no data was presented, I created the code by quoting sample data from other visualization libraries. And the group name needs to have a different name.
UPDATED:
import folium
import pandas as pd
import random
lat=[38.91427,38.91538,38.91458,38.92239,38.93222,38.90842,
38.91931,38.93260,38.91368,38.88516,38.921894,38.93206,38.91275]
lon=[-77.02827,-77.02013,-77.03155,-77.04227,-77.02854,-77.02419,
-77.02518,-77.03304,-77.04509,-76.99656,-77.042438,-77.02821,-77.01239]
street = ['PO Box 398 Buckland AK', '344 Watermelon St Holland Patnt NY', '162 Lime Ln #756 Aurora CO',
'PO Box 57 Alba MI', '185 Apple Blvd Burnett IN', 'PO Box 157 Hoytville OH','PO Box 184 Burnside PA',
'991 Watermelon Blvd #170 Memphis TN', '795 Papaya Ct #317 Parc Palenque PR2',
'646 Papaya Ctr #331 Urb Lirios Del Sur PR', 'PO Box 44 Marshfield MA','398 Cherry Dr #924 Toledo OR',
'730 Lime Ctr Rogers OH']
status = random.choices(['To be surveyed','Survey arranged','Survey Complete'], k=13)
post_code = [99727, 13354, 80047, 49611, 47805, 43529, 15721, 38115, 617, 716, 2059, 97391, 44455]
sp = ['OR']*13
df = pd.DataFrame({'sp':sp, 'street': street, 'post_code': post_code, 'status': status, 'lat': lat, 'lng': lon})
df.head()
sp street post_code status lat lng
0 OR PO Box 398 Buckland AK 99727 Survey Complete 38.91427 -77.02827
1 OR 344 Watermelon St Holland Patnt NY 13354 To be surveyed 38.91538 -77.02013
2 OR 162 Lime Ln #756 Aurora CO 80047 Survey arranged 38.91458 -77.03155
3 OR PO Box 57 Alba MI 49611 Survey Complete 38.92239 -77.04227
4 OR 185 Apple Blvd Burnett IN 47805 Survey Complete 38.93222 -77.02854
Data acquisition using itertools
# Use itertools
for i,row in df.iterrows():
print(type(row))
print(row['sp'])
print(row['street'])
print(row['post_code'])
print(row['status'])
print(row['lat'])
print(row['lng'])
<class 'pandas.core.series.Series'>
OR
PO Box 398 Buckland AK
99727
Survey Complete
38.91427
-77.02827
Data acquisition using df.at[].
# Use df.at[]
for i in range(len(df)):
print(type(df.at[i,'sp']))
print(df.at[i,'sp'])
print(df.at[i,'street'])
print(df.at[i,'post_code'])
print(df.at[i,'status'])
print(df.at[i,'lat'])
print(df.at[i,'lng'])
<class 'str'>
OR
PO Box 398 Buckland AK
99727
Survey Complete
38.91427
-77.02827
Use Itertools Full code
fo = folium.Map(location=[df['lat'].mean(),df['lng'].mean()], zoom_start=14)
fg = folium.FeatureGroup(name="OR", overlay=True)
for i in range(len(df)):
lat =df.at[i, 'lat']
lng = df.at[i, 'lng']
sp = df.at[i, 'sp']
stat = df.at[i,'status']
if stat =="To be surveyed":
style = '<strong style="text-transform:uppercase; color:red;">' + stat +'</strong>'
elif stat =="Survey arranged":
style = '<strong style="text-transform:uppercase; color:blue;">' + stat +'</strong>'
else:
style = '<strong style="text-transform:uppercase; color:green;">' + stat +'</strong>'
popup = df.at[i,'sp'] +'<br>' + df.at[i,'street'] + '<br>' + str(df.at[i,'post_code']) + '<br>{}' .format(style)
folium.Marker(
location=[lat, lng],
icon=folium.Icon(color='blue', icon='glyphicon-calendar'),
popup=popup
).add_to(fg)
fg.add_to(fo)
folium.LayerControl().add_to(fo)
fo
Use df.at[]
fo = folium.Map(location=[df['lat'].mean(),df['lon'].mean()], zoom_start=14)
fg = folium.FeatureGroup(name="OR", overlay=True)
for i,row in df.iterrows():
stat = row['status']
if stat == 'To be surveyed':
style = '<strong style="text-transform:uppercase; color:blue;">' + stat +'</strong>'
elif stat == 'Survey arranged':
style = '<strong style="text-transform:uppercase; color:red;">' + stat +'</strong>'
else:
style = '<strong style="text-transform:uppercase; color:green;">' + stat +'</strong>'
popup = row['sp'] +'<br>' + row['street'] + '<br>' + str(row['post_code']) + '<br>{}'.format(style)
folium.Marker(
location=[row['lat'], row['lon']],
icon=folium.Icon(color='blue', icon='glyphicon-calendar'),
popup=popup
).add_to(fg)
fg.add_to(fo)
folium.LayerControl().add_to(fo)
fo