Interactive Choropleth Map Using Python

590 Views Asked by At

I found a code for creating the worldmap that uses the shapefile from https://www.naturalearthdata.com/downloads/110m-cultural-vectors/ Download Countries v4.1.0

shapefile = 'data/countries_110m/ne_110m_admin_0_countries.shp'
datafile = 'data/obesity.csv'

gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']]
gdf.columns = ['country', 'country_code', 'geometry']
gdf.head()


    country                     country_code      geometry
0   Fiji                                 FJI      (POLYGON ((180 -16.06713266364245, 180 -16.555...
1   United Republic of Tanzania          TZA      POLYGON ((33.90371119710453 -0.950000000000000...
2   Western Sahara                       SAH      POLYGON ((-8.665589565454809 27.65642588959236...
3   Canada                               CAN      (POLYGON ((-122.84 49.00000000000011, -122.974...
4   United States of America             USA      (POLYGON ((-122.84 49.00000000000011, -120 49....

Many people use this file, and everything is ok. But when I open the file, it only has one column 'geometry'

KeyError: "['ADMIN', 'ADM0_A3'] not in index"

And I can't understand if something is wrong with the file, or is it something wrong with the code?

Update. I think the problem is with this error. Sometimes this works, sometimes this error appears

DriverError: Unable to open ne_110m_admin_0_countries.shx or ne_110m_admin_0_countries.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
2

There are 2 best solutions below

1
On

It seems like you renamed the columns:

gdf.columns = ['country', 'country_code', 'geometry']

where admin is now country and ADM0_A3 is now country_code

0
On

I solved the problem. To solve this error DriverError: Unable to open ne_110m_admin_0_countries.shx or ne_110m_admin_0_countries.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it. I just created a folder with 7 same files of different format: .cpg, .dbf, .prj, .README, .shp, .shx, .VERSION and used the .shp file from that directory, and the Jupyter was able to read that file correctly with all of the columns.