-1

I'm working on coding a folium map and I keep getting this error. I'm new to this and honestly have no idea how to fix this.

import folium

geojson_map = 'https://raw.gituserhubcontent.com/dirkkoolmees/maps-import-export-of-fossil-fuels-/master/custom.geo.json'

world_map = folium.Map(location=[0,0], zoom_start = 1.5, tiles = 'CartoDB positron', min_zoom = 1, max_zoom = 6)

folium.Choropleth(
    geo_data = geojson_map,
    name = "chloropleth",
    data = df_clean,
    columns = [df.index, 'value'],
    key_on= 'feature.properties.name_sort',
    fill_color = 'Reds',
    fill_opacity = 0.9,
    line_opacity = 0.2,
    legend_name = 'GDP'
    ).add_to(world_map)

world_map

this is the error I receive:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-b88f980d229d> in <module>()
     13     fill_color = 'Reds',
     14     fill_opacity = 0.9,
---> 15     line_opacity = 0.2,
     16     #legend_name = 'GDP'
     17     ).add_to(world_map)

1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
   4597                 # ensure_index_from_sequences would not raise for append=False.
   4598                 raise ValueError(
-> 4599                     f"Length mismatch: Expected {len(self)} rows, "
   4600                     f"received array of length {len(arrays[-1])}"
   4601                 )

ValueError: Length mismatch: Expected 154 rows, received array of length 324

Does anybody know how to fix this? Thank you!

1 Answers1

0
  • there are a few errors in your code
    1. URL to GitHub is wrong
    2. neither df_clean or df are defined
  • fixing these, the code works, noting there are 175 geometries in your GeoJSON
  • it's not clear what you are trying to achieve, but this code then works. Not sure why you are using df and df_clean that can have different numbers of rows
import folium
import numpy as np
import pandas as pd

geojson_map = 'https://raw.githubusercontent.com/dirkkoolmees/maps-import-export-of-fossil-fuels-/master/custom.geo.json'

world_map = folium.Map(location=[0,0], zoom_start = 1.5, tiles = 'CartoDB positron', min_zoom = 1, max_zoom = 6)

# code is using two different dataframes! create them...
df_clean = pd.DataFrame(index=range(175), data={"value":np.linspace(1,5,175)})
df = pd.DataFrame(index=range(175))

folium.Choropleth(
    geo_data = geojson_map,
    name = "chloropleth",
    data = df_clean,
    columns = [df.index, 'value'],
    key_on= 'feature.properties.name_sort',
    fill_color = 'Reds',
    fill_opacity = 0.9,
    line_opacity = 0.2,
    legend_name = 'GDP'
    ).add_to(world_map)

world_map
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30