我正在尝试使用通过合并包含巴西所有区域的shapefile(名称为“ map_1”)的shapefile与常规的熊猫数据框(名称为“ amazon_state”)而构建的geopandas数据框,在大叶草地图上添加一个Choropleth图层。合并两个数据框后,我得到的是“ 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:无法渲染缺少任何几何形状的对象。使用过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
答案 0 :(得分:0)
我有同样的问题。 map_3是地理数据框吗?如果没有,则必须先进行转换:
gdf = gpd.GeoDataFrame(map_3 , geometry = map_3.geometry)
gdf.crs = {'init' :'epsg:4326'}