I am trying to create a choropleth map using folium. However my code below keeps saying:
ValueError: Cannot render objects with any missing geometries: type features
The code used is:
url='https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/Canada.xlsx'
df_can=pd.read_excel(url, sheet_name='Canada by Citizenship', skiprows=range(20), skipfooter=2)
df_can.drop(['AREA','REG','DEV','Type','Coverage'], axis=1, inplace=True)
df_can.rename(columns={'OdName':'Country', 'AreaName':'Continent','RegName':'Region'}, inplace=True)
df_can.columns=list(map(str, df_can.columns))
df_can['Total']=df_can.sum(axis=1)
years=list(map(str, range(1980,2014)))
JSON='https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/world_countries.json'
world_geo=pd.read_json(JSON)
world_map=folium.Map(location=[0,0], zoom_start=2)
world_map.choropleth(
geo_data=world_geo,
data=df_can,
columns=['Country', 'Total'],
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Immigration to Canada'
)
# display map
world_map
Please help me to understand what is going wrong?
I think the cause of the error is that the geojson loading is a data frame. Normally, you would associate the geojson file with the data frame you want to visualize. The format of that association needs to be in dictionary format.
import json
from urllib.request import urlopen
JSON='https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/world_countries.json'
with urlopen(JSON) as f:
world_geo = json.load(f)
# world_geo=pd.read_json(JSON)
world_map = folium.Map(location=[0,0], zoom_start=2)
folium.Choropleth(
geo_data=world_geo,
data=df_can,
columns=['Country', 'Total'],
key_on='feature.properties.name',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Immigration to Canada'
).add_to(world_map)
# display map
world_map