6

I have dissolved polygons with geopandas. However, the head of attribute table became values of this table.

How can I fix this trouble?

See:

Raw data

After dissolve

1 Answer 1

13

Simple dissolve by GeoDataFrame field (Aggregation with dissolve)

import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
continents = world.dissolve('continent')
continents.head(3)

enter image description here

But if we use as_index=False from Pandas DataFrame.groupby

continents2 = world.dissolve('continent', as_index=False)
continents2.head(3)

enter image description here

Examine the GeoPandas test_dissolve.py script

New

It is a problem of indexing in DataFrame.groupby

import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
continents = world.dissolve('continent')
type(continents)
<class 'geopandas.geodataframe.GeoDataFrame'>
continents.index
Index([u'Africa', u'Antarctica', u'Asia', u'Europe', u'North America',
   u'Oceania', u'Seven seas (open ocean)', u'South America'],
  dtype='object', name=u'continent')

continents is a GeoDataFrame with a hierarchical index

With

 continents2 = world.dissolve('continent', as_index=False)

You eliminate the hierarchical index and you have the columns again

3
  • 1
    Very good! Thank you! What does it means "as_index"? Do you know? Feb 9, 2017 at 16:57
  • Look above in New
    – gene
    Feb 9, 2017 at 17:50
  • as_index=False is much better than resetting the index, thank you
    – Leo
    Jul 23, 2020 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.