使用Pandas'在GeodataFrames上应用()函数
我正在尝试做一件非常简单的事情。我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
axis = 1
https://pandas.pydata.org/docs/reference/reference/api/pandas.dataframe.apply.html ,因此您应用于每个列,而不是每行。axis=1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html, hence you are applying to each column, not each row.