Search code examples
pythongeopandas

How to aggregate duplicate rows that have the same geometries?


Below is a example of my data set. I want to combine duplicated points together and get the sum of values in columns a , b and c into a single row.

I have looked at a previous example using groupby.sum() here How do I Pandas group-by to get sum?. Because I am dealing with geometries I can't get my code to work.

geometry a b c
point a 2 4 6
point a 3 1 7
point b 1 2 3

This is want I want:

geometry a b c
point a 5 5 13
point b 1 2 3

Solution

  • Covert geometry to wtk:

    df = df([df['geometry'].to_wkt()], ).agg('a' : 'sum', 'b' : 'sum', 'c' :'sum').reset_index()

    Then back to geometry:

    df['index'] = gpd.GeoSeries.from_wkt(df['index'])

    df = gpd.GeoDataFrame(df)