Replacing ID values of polygons in a geodataframe to values of polygons from another geodataframe

117 Views Asked by At

I have polygons inside another bigger single polygon and I want to be able to replace the ID values (for example) of the former polygon to that of the latter.

Suppose I have a geodataframe with 3 polygons and the IDs of them are [8, 20, 55]. Now, these polygons are actually inside another bigger polygon (second geodataframe) with an ID of [2]. I want to know how it is possible to convert the values of [8,20,55] from the first geodataframe to [2,2,2] according to the bigger polygon from the second geodataframe. Conceptually the image looks like this:

this

1

There are 1 best solutions below

2
On

Use sjoin:

gdf1['ID'] = gdf1.sjoin(gdf2, how='left', predicate='within')['ID_right']