-2

I would like to create a Choropleth map in Python using Folium library.

This is the dataset:

df_new_count2

Reviewer Country Count Original Number Percentage

0 Greece 191 3352 0.056981

1 Belgium 338 5991 0.056418

2 Germany 429 7843 0.054698

The Json file used is the following: https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json

This is the code I used:

world_map = folium.Map(location=[40, 0], zoom_start=1.5)

folium.Choropleth( geo_data='https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json',

    name='choropleth',

    data=df_new_count2,

    columns=['Reviewer Country','Percentage'],

    key_on='feature.properties.name',

    fill_color='YlOrRd',

    fill_opacity=0.7,

    line_opacity=0.2,

    legend_name='Percentage of people'
).add_to(world_map)

folium.LayerControl().add_to(world_map)

world_map

The map is displaying but everything is black, the colors are not showing.

Map

Does anyone know what the issue might be? Thanks in advance for your time.

Matteo
  • 3
  • 1
  • 2

1 Answers1

0

You can try:

import pandas as pd
import folium

# create dataframe
df_new_count2 = pd.DataFrame([['Greece', 0.056981], ['Belgium', 0.056418], ['Germany', 0.054698]], columns=['Reviewer Country','Percentage'])
df_new_count2

# plot map
world_map = folium.Map(location=[40, 0], zoom_start=1.5)
folium.Choropleth(geo_data='https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json', name='choropleth',
    data=df_new_count2,
    columns=['Reviewer Country','Percentage'],
    key_on='feature.properties.name',
    fill_color='YlOrRd',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Percentage of people'
).add_to(world_map)
folium.LayerControl().add_to(world_map)
world_map

Result: enter image description here

It only colors the countries for which data is available in the dataframe. Can you validate your dataframe contains data for the countries selected?

René
  • 4,594
  • 5
  • 23
  • 52
  • Thanks for your reply. Unfortunately it does not change anything... – Matteo Feb 23 '20 at 12:56
  • The dataset comes from Kaggle and I used pandas to read it. I wanted to filter only the rows with 'Original Number' greater than 2000 so I used the following piece of code: df_new_count2=df_new_count[df_new_count['Original Number'] > 2000], this is the last instruction before creating the map... – Matteo Feb 23 '20 at 15:17
  • I just updated m answer. The map only colors the countries for which data is available in the dataframe. Can you validate your dataframe contains data for the countries selected? – René Feb 23 '20 at 16:26
  • 1
    Hi Rene, I figured out why it wasn't working. All the strings with the names of the countries had blank spaces, so I had to trim them. Thank you very much for your time and cooperation! – Matteo Feb 23 '20 at 17:23