[英]Can't Resolve: ValueError: Cannot render objects with any missing geometries
[英]ValueError: Cannot render objects with any missing geometries when using a geopandas dataframe
我正在尝试使用 geopandas 数据框在大叶地图上添加一个 choropleth 图层,该数据框是通过将包含巴西所有区域(名为“map_1”)的 shapefile 与常规熊猫数据框(名为“amazon_state”)合并而构建的。 2个数据帧的合并给了我“map_2”,在清理(删除一些行)之后我称之为“map_3”。
'''
#importing shapefile map_1, which contains all regions in Brazil
map_1= gpd.read_file("/Users/alexandertankou/Desktop/python/bra_adm1/BRA_adm1.shp")
# creating amazon_state
amazon_state= amazon_data.groupby("state", as_index=False).sum().drop(columns=["year"])
# ensure the naming of regions e in map_1 and amazon_state is the same
map_1.NAME_1= amazon_state.state
#map_2: merging map_1 with amazon_state
map_2= pd.merge (left=map_1, right= amazon_state, left_on="NAME_1", right_on="state", how= "left")
#dropping none useful columns
map_2= map_2.drop(columns=["NAME_0",'HASC_1',"ID_1","ISO","CCN_1","CCA_1","ID_0","TYPE_1","ENGTYPE_1", "NL_NAME_1","VARNAME_1"])
#ploting map_2
map_2.plot(column="number", cmap="YlOrRd",legend=True, figsize= (13,10))
#setting the folium map
m=folium.Map(location= [-22.919882,-43.604392], zoom_start=10)
#making geomery type hashable in python
map_2['geometry'] = map_2['geometry'].apply(lambda x: str(x))
#cleaning up map_2 data
map_3= map_2.iloc[0:23,:]
#add the choropleth layer on folium map
m.choropleth(geo_data= map_3,name="geometry", data= map_3,key_on="feature.properties.NAME_1",columns=["geometry","number"],fill_color='YlGn')
folium.LayerControl().add_to(m)
'''
但我不断收到ValueError: Cannot render objects with any missing geometry 。 使用 isnan 和 is_empty 方法后,我确定 map_3 中没有缺失值(请参阅下面的数据),所以不确定我做错了什么:
NAME_1 geometry \
0 Acre POLYGON ((-73.33251190185541 -7.32487916946411...
1 Alagoas MULTIPOLYGON (((-35.90152740478516 -9.86180496...
2 Amapa MULTIPOLYGON (((-50.02402877807612 0.859862029...
3 Amazonas POLYGON ((-67.32623291015625 2.029680967331046...
4 Bahia MULTIPOLYGON (((-38.69708251953125 -17.9790287...
5 Ceara MULTIPOLYGON (((-38.47541809082026 -3.70097303...
6 Distrito Federal POLYGON ((-48.03603363037109 -15.5002202987670...
7 Espirito Santo MULTIPOLYGON (((-40.88402938842768 -21.1612491...
8 Goias POLYGON ((-50.15817260742188 -12.4123792648315...
9 Maranhao MULTIPOLYGON (((-42.12374877929688 -2.80069398...
10 Mato Grosso POLYGON ((-56.1036376953125 -17.17354011535639...
11 Minas Gerais POLYGON ((-57.6052360534668 -8.662846565246525...
12 Paraiba POLYGON ((-44.20977783203119 -14.2366542816162...
13 Par· MULTIPOLYGON (((-46.43458175659174 -1.01708304...
14 Pernambuco MULTIPOLYGON (((-42.87873840332026 -9.29837322...
15 Piau MULTIPOLYGON (((-48.63069534301758 -25.8679161...
16 Rio MULTIPOLYGON (((-35.13597106933594 -8.83791732...
17 Rondonia POLYGON ((-41.81680679321283 -2.74375009536743...
18 Roraima MULTIPOLYGON (((-44.67124938964838 -23.3545837...
19 Santa Catarina MULTIPOLYGON (((-35.10902786254883 -6.19347190...
20 Sao Paulo MULTIPOLYGON (((-52.07069396972656 -32.0284729...
21 Sergipe POLYGON ((-63.53470230102539 -7.97433900833129...
22 Tocantins POLYGON ((-60.16886138916004 5.226301193237362...
state number
0 Acre 18464.030
1 Alagoas 4644.000
2 Amapa 21831.576
3 Amazonas 30650.129
4 Bahia 44746.226
5 Ceara 30428.063
6 Distrito Federal 3561.000
7 Espirito Santo 6546.000
8 Goias 37695.520
9 Maranhao 25129.131
10 Mato Grosso 96246.028
11 Minas Gerais 37475.258
12 Paraiba 52435.918
13 Par· 24512.144
14 Pernambuco 24498.000
15 Piau 37803.747
16 Rio 45160.865
17 Rondonia 20285.429
18 Roraima 24385.074
19 Santa Catarina 24359.852
20 Sao Paulo 51121.198
21 Sergipe 3237.000
22 Tocantins 33707.885
我有同样的问题。 map_3 是地理数据框? 如果没有,您必须先转换:
gdf = gpd.GeoDataFrame(map_3 , geometry = map_3.geometry)
gdf.crs = {'init' :'epsg:4326'}
@Thomas 是对的:您的问题是您的map_2
和map_3
变量对应于<class 'pandas.core.frame.DataFrame'>
,但您需要<class 'geopandas.geodataframe.GeoDataFrame'>
。
原因在这里:
map_2 = pd.merge(left=map_1, right=amazon_state, left_on="NAME_1",
right_on="state", how= "left")
当您调用pandas.merge()
方法并且您的参数是GeoDataFrame
和DataFrame
,结果将始终是DataFrame
。
如果您想在合并后获取GeoDataFrame
,请改用pandas.DataFrame.merge()
方法。 确保从GeoDataFrame
调用它, not DataFrame
:
map_2 = map_1.merge(right=amazon_state, left_on="NAME_1",
right_on="state", how="left")
您还可以查看 geopandas 的相关文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.