1

I have these columns:

index, area_of_poly, cad_admin

I have to group by index (it is a normal column called index) in order to take the rows that have the same value.

#all the ones, all the twos, etc

Some of them (rows) are unique though. About the ones that are not unique now:

What I have done so far: I have to check with a group by which of the groups have the largest area and give its respected cad_admin value to the others in its group in a new column called cad_admin1. The unique values are going to still have the same value they had in cad_admin in the now cad_admin1 column.

more info:

the dataset: http://www.mediafire.com/file/x4q5k7xuztq6o3w/p.zip

import geopandas as gpd
inte=gpd.read_file('in.shp')


inte['index'].value_counts()[inte['index'].value_counts()>1]


359    9
391    8
376    7
374    6
354    5
446    4
403    4
348    4
422    4
424    4
451    4
364    3
315    3
100    3
245    3







inte["rank_gr"] = inte.groupby("index")["area_of_poly"].rank(ascending = False, method = 
"first")
inte["key1_temp"] = inte.apply(lambda row: str(row[""]) if row["rank_gr"] == 1.0
else "", axis = 1)
inte["CAD_ADMIN_FINAL"] = inte.groupby("index")["key1_temp"].transform("sum")
print (inte[["area_of_poly", "index", "CAD_ADMIN", "CAD_ADMIN_FINAL"]])

This code as you will see produces some errors like:

TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

KeyError: ('', 'occurred at index 0')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.