Attribute joins are accomplished using the merge()
method. In general, it is recommended to use the merge()
method called from the spatial dataset. With that said, the stand-alone pandas.merge()
function will work if the GeoDataFrame
is in the left
argument; if a DataFrame
is in the left
argument and a GeoDataFrame
is in the right
position, the result will no longer be a GeoDataFrame
.
属性连接使用 merge() 方法完成。一般来说,建议使用通过空间数据集调用 merge() 方法。话虽如此,如果 GeoDataFrame 在左侧参数中,则独立的 pandas.merge() 函数将起作用;如果 DataFrame 在左边的参数中, GeoDataFrame 在右边的位置,则结果将不再是 GeoDataFrame。
For example, consider the following merge that adds full names to a GeoDataFrame
that initially has only ISO codes for each country by merging it with a DataFrame
.
例如,考虑以下合并,将全名添加到 GeoDataFrame,该 GeoDataFrame 最初只有每个国家/地区的 ISO 代码。
# `country_shapes` is GeoDataFrame with country shapes and iso codes
In [11]: country_shapes.head()
Out[11]:
geometry iso_a3
0 MULTIPOLYGON (((180.000000000 -16.067132664, 1... FJI
1 POLYGON ((33.903711197 -0.950000000, 34.072620... TZA
2 POLYGON ((-8.665589565 27.656425890, -8.665124... ESH
3 MULTIPOLYGON (((-122.840000000 49.000000000, -... CAN
4 MULTIPOLYGON (((-122.840000000 49.000000000, -... USA
# `country_names` is DataFrame with country names and iso codes
In [12]: country_names.head()
Out[12]:
name iso_a3
0 Fiji FJI
1 Tanzania TZA
2 W. Sahara ESH
3 Canada CAN
4 United States of America USA
# Merge with `merge` method on shared variable (iso codes):
In [13]: country_shapes = country_shapes.merge(country_names, on='iso_a3')
In [14]: country_shapes.head()
Out[14]:
geometry ... name
0 MULTIPOLYGON (((180.000000000 -16.067132664, 1... ... Fiji
1 POLYGON ((33.903711197 -0.950000000, 34.072620... ... Tanzania
2 POLYGON ((-8.665589565 27.656425890, -8.665124... ... W. Sahara
3 MULTIPOLYGON (((-122.840000000 49.000000000, -... ... Canada
4 MULTIPOLYGON (((-122.840000000 49.000000000, -... ... United States of America
[5 rows x 3 columns]