# GeoPandas statistics of points within a multi polygon (grid) [duplicate]

I usually use numpy for my processing but I'm trying to make the most of the power of GeoPandas for my spatial data. I have some point data (shapefile) and a vector grid (fishnet) as a separate shapefile. I would like to get the points that fall within each vector grid and then do some basic statistics on them (e.g., average). Each point has an X co-ord, y co-ord and a Z value. I would like the mean value of each that falls within a grid cell.

All answers I have seen so far are either testing if one point is within a polygon, or does not go further and calculate statistics, only returning a boolean array.

From this answer I got this piece of code:

``````point = geopandas.GeoDataFrame.from_file(pointfile.shp)
poly  = geopandas.GeoDataFrame.from_file(gridfile.shp)

pointInPolys = sjoin(point, poly, how='left')
grouped = pointInPolys.groupby('index_right')
``````

but as I'm pretty new to Pandas I'm not sure how to get at the data in this group (or really what a group is) so I can calculate the mean value of the points. How do I get the data and perform basic statistics on it?

• – gene
Commented Jun 13, 2020 at 8:12
• @KadirŞahbaz X, Y and Z are separate columns Commented Jun 15, 2020 at 14:45

A `groupby` has the same underlying data from the dataframe, but adds an index on how to group operations.
In your example, running `grouped.mean()` returns the average on every group for every column. The concept is very close to SQL GROUPBY.
You can also add the group average to the original dataset using `point["cell_avg"] = grouped.transform(np.mean)`, it works just like OVER PARTITION on a db.