【问题标题】:GeoPandas plot shapefile by ignoring some administrative areasGeoPandas 通过忽略一些行政区域来绘制 shapefile
【发布时间】:2021-06-22 19:47:16
【问题描述】:

Shapefile 数据:来自https://gadm.org/data.html的整个世界(有 5 个行政区域

import geopandas as gpd
World = gpd.read_file("~/gadm36.shp")
World=World[['NAME_0','NAME_1','NAME_2','geometry']] #Keep only 3 columns
World.head()

在这个 GeoDataFrame 中,我有 60 列NAME_0:代表国家名称,NAME_1代表地区,...)

目前,我有兴趣研究我的网站在德国的用户数量

Germany=World[World['NAME_0'].isin(['Germany']) == True]

现在我的网站用户数据按区域 (NAME_1),我将第一列重命名为 shapefile 中的相同

GER = pd.read_csv("~/GER.CSV",sep=";") 
GER

现在我将我的数据合并到 NAME_1 上的 GeoDataFrame 以绘制区域中的用户

merged_ger = Germany.merge(GER, on = 'NAME_1', how='left') 
merged_ger['Users'] = merged_ger['Users'].fillna(0)

这里的问题是 NAME_1 根据 NAME_2 重复。因此,合并数据中的用户总数大大超过了原始数量

print(merged_ger['Users'].sum())
print(GER['Users'].sum())

7172411.0
74529

所以使用此代码绘制数据

import matplotlib.pyplot as plt
merged_ger.plot(column='Users')

显然错了

在这种情况下,如何在不重复且不影响最终绘图的情况下合并数据? 或者,如何忽略 shapefile 中的其余管理区域?

【问题讨论】:

    标签: python matplotlib plot geopandas


    【解决方案1】:

    映射用户区域的字典不会有帮助吗?

    GER_users = dict(zip(GER.NAME_1, GER.Users))
    Germany['Users'] = Germany['NAME_1'].map(GER_users)
    

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 2019-08-12
      • 2018-10-26
      • 2018-10-06
      • 1970-01-01
      • 2019-12-22
      相关资源
      最近更新 更多