Let's assume there is polygon layer (a shapefile) with the following attribute table, see image below

With this code I am loading this shapefile into a GeoDataFrame
import geopandas as gpd
file = "P:/Test/qgis_test/test_for_geopandas.shp"
gdf = gpd.read_file(file)
print(gdf)
The GeoDataFrame itself
fid ... geometry
0 6.0 ... POLYGON ((233499.352 5752838.208, 559980.331 5...
1 7.0 ... POLYGON ((233499.352 5752838.208, 559980.331 5...
2 8.0 ... POLYGON ((233499.352 5752838.208, 559980.331 5...
3 9.0 ... POLYGON ((978501.160 5695530.377, 1164317.462 ...
4 10.0 ... POLYGON ((978501.160 5695530.377, 1164317.462 ...
5 11.0 ... POLYGON ((978501.160 5695530.377, 1164317.462 ...
6 12.0 ... POLYGON ((978501.160 5695530.377, 1164317.462 ...
7 13.0 ... POLYGON ((978501.160 5695530.377, 1164317.462 ...
8 14.0 ... POLYGON ((978501.160 5695530.377, 1164317.462 ...
9 15.0 ... POLYGON ((485306.490 4940108.963, 681542.397 5...
[10 rows x 4 columns]
With the following code, it is possible to keep only the max "flood_score"
data for each unique geometry
.
gdf_ = gdf.sort_values('flood_scor', ascending=False).drop_duplicates(['geometry'])
print(gdf_)
The output GeoDataFrame will look like:
fid ... geometry
8 14.0 ... POLYGON ((978501.160 5695530.377, 1164317.462 ...
1 7.0 ... POLYGON ((233499.352 5752838.208, 559980.331 5...
9 15.0 ... POLYGON ((485306.490 4940108.963, 681542.397 5...
[3 rows x 4 columns]
geometry
, getting only the maximumflood_score
. When I spatially joined the two maps, it resulted in duplicategeometry
data with differentflood_score
values. For example, there are two rows withgeometry
x, but with differentflood_score
data because they both intersected. I only want one instance ofgeometry
x that has the maximum of theflood_score
data.drop_duplicates
drops rows from a top-down order based on the sorted dataframe, then this should be correct. You can post this as an answer and I'll mark it. Thank you!