1. 主页
  2. 文档
  3. GeoPandas 0.12.2中文文档
  4. User Guide
  5. Aggregation with dissolve(聚合操作)
  6. dissolve() Example

dissolve() Example

Suppose we are interested in studying continents, but we only have country-level data like the country dataset included in geopandas. We can easily convert this to a continent-level dataset.

假设我们对研究大陆感兴趣,但我们只有国家级数据,例如 geopandas 中包含的国家数据集。我们可以轻松地将其转换为大陆级别的数据集。

First, let’s look at the most simple case where we just want continent shapes and names. By default, dissolve() will pass 'first' to groupby.aggregate.

首先,让我们看一下我们只需要大陆形状和名称的最简单的情况。默认情况下,dissolve() 会将“first”传递给 groupby.aggregate。

In [1]: world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

In [2]: world = world[['continent', 'geometry']]

In [3]: continents = world.dissolve(by='continent')

In [4]: continents.plot();

In [5]: continents.head()
Out[5]: 
                                                        geometry
continent                                                       
Africa         MULTIPOLYGON (((-11.43878 6.78592, -11.70819 6...
Antarctica     MULTIPOLYGON (((-61.13898 -79.98137, -60.61012...
Asia           MULTIPOLYGON (((48.67923 14.00320, 48.23895 13...
Europe         MULTIPOLYGON (((-53.55484 2.33490, -53.77852 2...
North America  MULTIPOLYGON (((-155.22217 19.23972, -155.5421...

If we are interested in aggregate populations, however, we can pass different functions to the dissolve() method to aggregate populations using the aggfunc = argument:

如果我们对聚集的人口感兴趣,我们可以使用aggfunc参数向dissolve()方法传递不同的函数以聚集人口。

In [6]: world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

In [7]: world = world[['continent', 'geometry', 'pop_est']]

In [8]: continents = world.dissolve(by='continent', aggfunc='sum')

In [9]: continents.plot(column = 'pop_est', scheme='quantiles', cmap='YlOrRd');

In [10]: continents.head()
Out[10]: 
                                                        geometry       pop_est
continent                                                                     
Africa         MULTIPOLYGON (((-11.43878 6.78592, -11.70819 6...  1.306370e+09
Antarctica     MULTIPOLYGON (((-61.13898 -79.98137, -60.61012...  4.490000e+03
Asia           MULTIPOLYGON (((48.67923 14.00320, 48.23895 13...  4.550277e+09
Europe         MULTIPOLYGON (((-53.55484 2.33490, -53.77852 2...  7.454125e+08
North America  MULTIPOLYGON (((-155.22217 19.23972, -155.5421...  5.837560e+08
标签 , ,
这篇文章对您有用吗?

我们要如何帮助您?

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here