alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[1080] Remove duplicated records based on a specific column in GeoPandas

To remove duplicated records based on a specific column in GeoPandas, you can use the drop_duplicates method. Here's how you can do it:

Example Script

import geopandas as gpd
from shapely.geometry import Point
# Sample GeoDataFrame
data = {
'ID': [1, 2, 2, 3, 4, 4, 4],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace'],
'geometry': [Point(1, 2), Point(2, 3), Point(3, 4), Point(4, 5), Point(5, 6), Point(6, 7), Point(7, 8)]
}
gdf = gpd.GeoDataFrame(data, crs="EPSG:4326")
# Remove duplicated records based on the 'ID' column
gdf_cleaned = gdf.drop_duplicates(subset='ID', keep='first')
print(gdf_cleaned)

Explanation:

  • Import Libraries: Import geopandas and Point from shapely.geometry.
  • Create a Sample GeoDataFrame: Define a GeoDataFrame with a column (ID) that contains duplicate values.
  • Drop Duplicates: Use the drop_duplicates method with the subset parameter set to the column of interest (ID in this case). The keep='first' parameter ensures that only the first occurrence of each duplicate is retained. You can also use keep='last' to keep the last occurrence or keep=False to drop all duplicates.

Result:

This script will return a GeoDataFrame with duplicates removed based on the specified column.

Example Output:

ID Name geometry
0 1 Alice POINT (1.00000 2.00000)
1 2 Bob POINT (2.00000 3.00000)
3 3 David POINT (4.00000 5.00000)
4 4 Eve POINT (5.00000 6.00000)

In this example, only the first occurrence of each ID is kept, and all subsequent duplicates are removed.

Feel free to try this out, and let me know if you need any further assistance or have any other questions!

posted on   McDelfino  阅读(10)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2014-12-04 【156】我的工资条
2011-12-04 【006】◀▶ C#学习(五) - Winform<1>
点击右上角即可分享
微信分享提示