使用Pandas'在GeodataFrames上应用()函数

发布于 2025-01-18 06:03:09 字数 701 浏览 4 评论 0原文

我正在尝试做一件非常简单的事情。我有一个 geopandas 地理数据框 (gdf),其中包含一个属性列 (gdf.att) 和一个几何列 (gdf.geometry),所有几何图形都是形状点)。

现在我想创建一个额外的列,其值是点坐标和属性的函数的结果。我尝试使用 apply() 来执行此操作,如下所示:

gdf['new_col'] = gdf.apply(lambda row : function([row.geometry.x, row.geometry.y], row.att))

但是,我收到以下错误:

AttributeError: 'Series' object has no attribute 'geometry'

似乎在 apply 函数中, 是一系列我无法调用几何图形的序列。但我不明白这一点,因为如果我运行下面的测试来检查如果我只返回 row 会得到什么,那么我会得到一个 GeoDataFrame 作为输出。

test = gdf.apply(lambda row: row) # after running this, test is the same as gdf

有人知道如何将 apply() 与使用多列的 GDF 一起使用吗?

谢谢!

I am trying to do a very simple thing. I have a geopandas geodataframe (gdf) with an attribute column (gdf.att) and a geometry column (gdf.geometry, all geometries are shapely points).

Now I want to create an extra column whose values are the result of a function of the point coordinates and the attribute. I'm trying to do this using apply() as below:

gdf['new_col'] = gdf.apply(lambda row : function([row.geometry.x, row.geometry.y], row.att))

However, I get the following error:

AttributeError: 'Series' object has no attribute 'geometry'

It seems that within the apply function, row is a series of which I cannot call the geometry. But I don't understand this because if I run the test below to check what I get if I just return row, then I get a GeoDataFrame as output.

test = gdf.apply(lambda row: row) # after running this, test is the same as gdf

Does someone know how to use apply() with GDF's using multiple columns?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2025-01-25 06:03:09
import geopandas as gpd

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_cities')).rename(columns={"name":"att"})

def function(point, att):
    return att[int(point[0] % len(att))]

gdf['new_col'] = gdf.apply(lambda row : function([row.geometry.x, row.geometry.y], row.att), axis=1)

gdf.head(5)
att几何new_col
0梵蒂冈城区(12.453386544971766 41.90328217960115)1
SANMARINO(12.441770157800141 77429357)O
LuxembourgPointZ3
4POINT(6.130002806227083 49.611660379121076)Point
VPalikir(158.1499743237623 6.9166643696007725)k
import geopandas as gpd

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_cities')).rename(columns={"name":"att"})

def function(point, att):
    return att[int(point[0] % len(att))]

gdf['new_col'] = gdf.apply(lambda row : function([row.geometry.x, row.geometry.y], row.att), axis=1)

gdf.head(5)
attgeometrynew_col
0Vatican CityPOINT (12.453386544971766 41.903282179960115)V
1San MarinoPOINT (12.441770157800141 43.936095834768004)n
2VaduzPOINT (9.516669472907267 47.13372377429357)z
3LuxembourgPOINT (6.130002806227083 49.611660379121076)o
4PalikirPOINT (158.1499743237623 6.916643696007725)k
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文