Reputation: 21
I am trying to add a choropleth layer on a folium map using a geopandas dataframe that I built by merging a shapefile containing all the regions in Brazil (named "map_1"), with a regular pandas dataframe (named "amazon_state"). The merging of the 2 dataframes gives me "map_2", which after cleaning (removal of some rows) I referred to as "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)
'''
But I keep getting the ValueError: Cannot render objects with any missing geometries. Having used isnan and is_empty methods, I know for sure there are no missing values in map_3 (see the data below) so not sure what I am doing wrong:
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
Upvotes: 2
Views: 2512
Reputation: 11
@Thomas is right: your problem is your map_2
and map_3
variables corresponds to <class 'pandas.core.frame.DataFrame'>
, but you need <class 'geopandas.geodataframe.GeoDataFrame'>
.
The reason is here:
map_2 = pd.merge(left=map_1, right=amazon_state, left_on="NAME_1",
right_on="state", how= "left")
When you call pandas.merge()
method and your arguments are GeoDataFrame
and DataFrame
the result will be always DataFrame
.
If you want to get GeoDataFrame
after merging use pandas.DataFrame.merge()
method instead. Make sure you call it from the GeoDataFrame
, not DataFrame
:
map_2 = map_1.merge(right=amazon_state, left_on="NAME_1",
right_on="state", how="left")
You could also check relating documentation for geopandas.
Upvotes: 1
Reputation: 5
i had the same problem. map_3 is a geodataframe ? if not, you have to convert before :
gdf = gpd.GeoDataFrame(map_3 , geometry = map_3.geometry)
gdf.crs = {'init' :'epsg:4326'}
Upvotes: 0