1

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?

2

1 Answer 1

2

Once you've performed the spatial join, you can operate with your dataframe as in plain pandas.

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.

Docs on pandas groupby: https://pandas.pydata.org/pandas-docs/stable/reference/groupby.html

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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.