Search code examples
pythonmapsdata-visualizationfoliumchoropleth

Currency GeoJsonTooltip formatting in Folium


I am trying to format GeoJsonTooltip in Folium to display integer number as currency (Thousands separated with comma and 'Kč' string at the end.) Example:

INPUT: 1250000

WANTED OUTPUT: 1,250,000 Kč

I cannot do this in Python via string formatting, because GeoJsonTooltip cannot display String tooltips and ends up with following error:

(TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'')

I have not been able to come up with any solution to this problem. The code to create the map:

map_choropleth = folium.Map(location=[49.724,15.534],tiles='cartodbpositron', zoom_start=8, min_zoom=8, max_zoom=8, zoom_control=False)
choropleth = folium.Choropleth(geo_data = geojson_countries,
                  data = kraj_stats,
                  columns=['NAZEV_NUTS', 'cena_m2_mean'],
                  key_on='properties.NAZEV_NUTS',
                  fill_color='YlGn',
                  fill_opacity=0.7,
                  line_opacity=0.2,
                  legend_name='Cena za m2 v Kč'
                  ).add_to(map_choropleth)

choropleth.geojson.add_child(folium.features.GeoJsonTooltip(
        fields=['NAZEV_NUTS','cena_mean', 'cena_m2_mean'],
        aliases=['Název kraje', 'Průměrná cena', 'Průměrná cena za m2'],
        style=('background-color: grey; color: white;')
        )
)

map_choropleth.save("mymap.html")

And this is what i have currently:

enter image description here

Could anybody help, please?


Solution

  • I was unable to set currency formatting in CZK, however I was able to set numbers spacing by using localize=True.

    map_choropleth = folium.Map(location=[49.724,15.534],tiles='cartodbpositron', zoom_start=8, min_zoom=8, max_zoom=8, zoom_control=False)
    choropleth = folium.Choropleth(geo_data = geojson_countries,
                      data = merged_areas,
                      columns=['NAZEV_LAU1', 'cena_m2_mean'],
                      key_on='properties.NAZEV_LAU1',
                      fill_color='YlGn',
                      fill_opacity=0.85,
                      line_opacity=0.2,
                      legend_name='Cena za m2 v Kč'
                      ).add_to(map_choropleth)
    
    choropleth.geojson.add_child(folium.features.GeoJsonTooltip(
            fields=['NAZEV_LAU1','cena_mean', 'cena_m2_mean'],
            aliases=['Název okresu', 'Průměrná cena [Kč]', 'Průměrná cena za m2 [Kč]'],
            style=('background-color: grey; color: white;'),
            localize=True
            )
    )
    
    map_choropleth.save("mymap_okresy.html")