0

From the following files I want to do a map plot by using folium:

Polygons file: https://drive.google.com/file/d/1uhj7OyHktPseR_CtRD0vPfq8HziHA4RK/view? Postcode variable level: https://drive.google.com/file/d/1oFBITKzqLyTvQEASmZmO6CGCOzdPacgH/view?

Attempt1 (folium):

import pandas as pd
import geopandas as gpd
import folium

df_postcode_variable = pd.read_csv("postcode_sample.csv", dtype=str)  # Postcode variable level
df_postcode_variable['people'] = df_postcode_variable['people'].astype(float)
df_polygons = gpd.read_file("postcode_polygons.shp") # Polygons file
df_polygons.crs = "EPSG:4326"

my_map = folium.Map(location=[41.3890727,2.1572654], zoom_start=13,
                   detect_retina=True, control_scale=False)

folium.Choropleth(
        geo_data=df_polygons,
        name='choropleth',
        data=df_postcode_variable,
        columns=['cp','people'],
        key_on='cp',
        fill_color='OrRd',
        fill_opacity=0.5,
        line_opacity=0.8,
        line_color='Blue',
        legend_name='number of people'
    ).add_to(my_map)

my_map

But I get the following error:

ValueError: key_on `'cp'` not found in GeoJSON.

Attempt 2(pandas_bokeh) working:

import pandas_bokeh
pandas_bokeh.output_notebook()

df_joined=df_polygons.merge(df_postcode_variable.set_index('cp'), on='cp')

df_joined.plot_bokeh(simplify_shapes=20000,
                  category="people", 
                  colormap="Spectral", 
                  hovertool_columns=["cp","people"])

enter image description here

How could I fix this error on folium? cp is in both objects so, It doesnt make sense to me. It seems that is not thetecting cp in df_polygons.

PeCaDe
  • 277
  • 1
  • 8
  • 33
  • Please note that `pandas_bokeh` is a separate project, maintained by people who are not on the Bokeh core team. I have updated the post/tags accordingly. – bigreddot Jun 22 '22 at 19:49

2 Answers2

1

If the geodata is in geopandas format, the key is specified as feature.properties.column_name, as in geojson. See this for details.

my_map = folium.Map(location=[41.3890727,2.1572654], zoom_start=12,
                   detect_retina=True, control_scale=False)

folium.Choropleth(
        geo_data=df_polygons,
        name='choropleth',
        data=df_postcode_variable,
        columns=['cp','people'],
        key_on='feature.properties.cp',
        fill_color='OrRd',
        fill_opacity=0.5,
        line_opacity=0.8,
        line_color='Blue',
        legend_name='number of people'
    ).add_to(my_map)

my_map

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • great!, it was a simle thing but no very intuitive. I know it is another question but, Any insight on why pandas_bokeh is plotting the poygons badly? – PeCaDe Jun 27 '22 at 07:18
  • 1
    If you feel that the color visualization you are setting is too weak, you can choose a different color map. Or you can create your own. For more information. [See this](https://nbviewer.org/github/python-visualization/folium/blob/main/examples/Colormaps.ipynb), I haven't used Bokeh, but from my research it seems that `simplify_shapes` allows you to determine geo layers in meters. I haven't tried it, but a smaller value might make the polygons more accurate. I got the basis from [here](https://github.com/PatrikHlobil/Pandas-Bokeh). – r-beginners Jun 27 '22 at 07:56
0

This error message hints at an unexpected data type in your data which is causing np.isnan to complain somewhere under the hood, probably string.

Try changing the data type of your people column to int:

df_postcode_variable['people'] = df_postcode_variable['people'].astype(int)
rvd
  • 346
  • 2
  • 7
  • Hello rvd, you have provided some example data to check that the solution works, in your answer, you suggest a conversion to int, but it is to float that works directly. Still the error prevails, the problem is not solved, so this is more a comment than an answer. – PeCaDe Jun 23 '22 at 06:21