【问题标题】:Interactive Choropleth Map Using Python使用 Python 的交互式 Choropleth 地图
【发布时间】:2021-05-08 18:22:51
【问题描述】:

我找到了一个创建世界地图的代码,它使用来自https://www.naturalearthdata.com/downloads/110m-cultural-vectors/ 的 shapefile 下载国家 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....

很多人使用这个文件,一切正常。 但是当我打开文件时,它只有一列'geometry'

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

我不明白是文件有问题,还是代码有问题?

更新。我认为问题出在这个错误上。有时这可行,有时会出现此错误

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.

【问题讨论】:

    标签: python shapefile geopandas country world-map


    【解决方案1】:

    我解决了这个问题。解决这个错误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. 我刚刚创建了一个包含 7 个不同格式的相同文件的文件夹:.cpg、.dbf、.prj、.README、.shp、.shx、.VERSION 并使用该目录中的 .shp 文件,Jupyter 能够正确读取该文件的所有列。

    【讨论】:

      【解决方案2】:

      您似乎重命名了这些列:

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

      admin 现在是 countryADM0_A3 现在是 country_code

      【讨论】:

      • 当我运行代码 gdf = gpd.read_file(shapefile) 时,shapefile 本身只显示一列 'geometry',但应该有其他列。我不明白为什么会这样?
      猜你喜欢
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多