Unknown format code 'f' for object of type 'str'- Folium

Publish date: 2024-04-13

I have a dataframe that looks like below

 Number Names latitude longitude 0 1 Josh 25.713277 80.746531 1 2 Jon 25.713277 80.746531 2 3 Adam 25.713277 80.746531 3 4 Barsa 25.713277 80.746531 4 5 Fekse 25.713277 80.746531 5 6 Bravo 25.713277 80.746531 6 7 Levine 25.713277 80.746531 7 8 Talyo 25.713277 80.746531 8 9 Syden 25.713277 80.746531 9 10 Zidane 25.713277 80.746531 

I am trying to create a folium map for this dataframe, I wanted the Number column values to be displayed in some color based on the values of Names column with the following code, basically I want the number 1 to 10 to be displayed in some color for a place based on the Names. For example, 1 should be displayed in lightblue, 2 should be displayed in green color and rest of the numbers should be displayed in red color

for Number,Names,latitude,longitude in zip(dsa['Number'],dsa['Names'],dsa['latitude'],dsa['longitude']): folium.Marker(location=[latitude,longitude], icon=folium.DivIcon( html=f"""<div style="font-family: courier new; color: {'lightblue' if Names == 'Josh' else 'green' if Names == 'Jon' else 'red'}">{"{:.0f}".format(Number)}</div>""") ).add_to(m) m.save(os.path.join('color_popups1231.html')) 

But when I execute this I am getting this error:

ValueError: Unknown format code 'f' for object of type 'str' 

What am I missing here?

2

1 Answer

The f format code in "{:.0f}".format(Number) for the Python string formatter requires a floating number, and yet you're passing to it the variable Number, which is derived from dsa['Number'], a string value from the dataframe. You should convert Number to a floating number before passing it to the formatter with "{:.0f}".format(float(Number)) instead.

0

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobGtvZWeDdoCOrqWkpp%2Bsu26yzqukmqxdmLylsYyfZJ%2BnomK8o7bEnKtmp5Ziwbq8xGaqrapdm7yttdSm