I have a DataFrame
that resulted from spatially joining a digital elevation map with a square grid map.
This unexpectedly resulted in duplicate rows where two rows will have the same "geometry"
but a different "Elevation"
value.
How do I get the median of "Elevation"
for each unique "geometry"
? I'm new to GeoPandas, so I tried the traditional methods of aggregating a DataFrame
, but found that "geometry"
cannot be operated with the groupby()
function.
mrkna_grid.groupby("geometry")['Elevation'].median()
I have also tried using the dissolve()
function, but I don't think I'm doing it correctly because the number of rows were reduced to just seventy (70) as opposed to the original two thousand (2000) before the spatial join.
mrkna_grid.dissolve(by="Elevation", aggfunc="median")
Best Answer
Let's assume there is a polygon layer 'layer' with its attribute table, see the image below.
The expected result of median is:
Using one of the following code:
Solution #1 includes Pandas/GeoPandas native
DataFrame.median()
Solution #2 includes Python's
statistics.median()
it is possible to achieve the following output:
References: