我想知道是否有人能帮我这个,可能是天真的,问题,拜托?事先谢谢你的意见。问:我如何用群比按“id”、“几何学”分组?假设地质灾害数据读取: pts =
id prix agent_code geometry
0 922769 3000 15 POINT (3681922.790 1859138.091)
1 1539368 3200 26 POINT (3572492.838 1806124.643)
2 922769 50 15 POINT (3681922.790 1859138.091)
3 1539368 200 26 POINT (3572492.838 1806124.643)
我用过这样的方法:
pts = pts.groupby(['id', 'geometry']).agg(prom_revenue=('prix',np.mean))..reset_index()
但是,Python会引发以下错误:
TypeError: '<' not supported between instances of 'Point' and 'Point'
谢谢你的帮助,伙计们!
发布于 2022-05-03 16:40:10
使用to_wkt
from geometry
列将形状转换为纯文本:
out = pts.groupby(['id', pts['geometry'].to_wkt()], as_index=False) \
.agg(prom_revenue=('prix', np.mean))
print(out)
# Output
id prom_revenue
0 922769 1525.0
1 1539368 1700.0
发布于 2022-05-03 17:12:34
如果你把科伦的回答再往前走一步,你就可以像这样把分数再还原一遍:
out = pts.groupby(['id', pts['geometry'].to_wkt()]).agg(prom_revenue=('prix', np.mean)).reset_index()
out.columns = ['id', 'geometry', 'pro_revenue']
out['geometry'] = gp.GeoSeries.from_wkt(out['geometry'])
out = gp.GeoDataFrame(out)
print(out)
输出:
id geometry pro_revenue
0 922769 POINT (3681922.790 1859138.091) 1525.0
1 1539368 POINT (3572492.838 1806124.643) 1700.0
https://stackoverflow.com/questions/72102548
复制相似问题