# How aggregate data in a geodataframe by the geometry in a geoseries

I have a geoseries where each row is a (square) polygon. Housing price data is in a geodataframe where the geometry is a polygon of the zip code.

I need to aggregate the housing price data by squares in the geoseries.

For example for a square that is within a zip code the value would be the housing price of the zip code. When the square in the geoseries intersects many zip codes I would like to get the weighted average of the housing prices in the zip codes, where the weights would be the shares of the areas intersected by each zip code. (Or if this weighted average is difficult, I could take the price from the zip code whose share is greatest in the square)

Intersect the dataframes then groupby and calculate weighted mean using intersection areas as weights.

Example:

import geopandas as gpd
import pandas as pd
import numpy as np

price_column = 'price'
zip_area_id_column = 'county_id'

inter = gpd.overlay(zip_areas,prices)
inter['area'] = inter.area

wm = lambda x: np.average(x, weights=inter.loc[x.index, "area"]) #https://stackoverflow.com/questions/31521027/groupby-weighted-average-and-sum-in-pandas-dataframe
f = {price_column: {'weighted_mean' : wm} }
newdf = inter.groupby(zip_area_id_column).agg(f)
newdf.columns = newdf.columns.droplevel()


Then join/merge newdf back to the zip polygons if you want.

newdf
weighted_mean
county_id
1415          142.841362
1439          285.705400
1440          372.325510
1442          388.012432
1444          319.763218
1445          464.308318
1462          273.379472
1485          256.614212
1487          189.018582
1488          353.688960
1489          268.873202
1494          240.251814 