注意

This page was generated from gallery/pandas_accessor.ipynb.
Interactive online version: Binder badge

通过pandas的“geo”访问器使用GeoPandas#

GeoPandas 可以通过“geo”访问器从 pandas 中使用,该访问器在首次导入 geopandas.accessors 时注册在 pandas.Series 上。

[1]:
# Import geopandas.accessors to register the "geo" accessor.
import geopandas.accessors  # noqa # pylint: disable=unused-import
import pandas as pd
from shapely import Point

使用 Series.geo 访问器#

使用GeometryDtype构造一个Series。

[2]:
s = pd.Series(
    [Point(1, 2), Point(3, 4), Point(5, 6)],
    dtype='geometry',
)
s
[2]:
0    POINT (1 2)
1    POINT (3 4)
2    POINT (5 6)
dtype: geometry

所有 GeoSeries 方法和属性都可以通过“geo”访问器获得。

[3]:
s.geo.x
[3]:
0    1.0
1    3.0
2    5.0
dtype: float64
[4]:
s2 = pd.Series(
    [Point(-1, -2), Point(-3, -4), Point(-5, -6)],
    dtype='geometry',
)
s.geo.distance(s2)
[4]:
0     4.472136
1    10.000000
2    15.620499
dtype: float64