I am trying to use Python and GeoPandas to create variable distance buffers for a polygons shapefile I have. I brought this shapefile into python using GeoPandas. One of the columns of the GeoDataFrame has a Distances
column with a unique distance for each polygon (row). I want to buffer all of the polygons in the shapefile/GeoDataFrame, but where each individual polygon is buffered at a distance corresponding to its own corresponding distance in the Distances
column.
I tried this:
polygons = gpd.read_file('polygons.shp')
distances = polygons['Distances']
polygons_buffered = polygons.buffer(distances)
polygons_buffered.to_file("polygons_buffered.shp")
polygons_buffered = gpd.read_file('polygons_buffered.shp')
polygons_buffered.head()
And then I see that the only data that is preserved in the attribute table is and FID and geometry, but all of my attribute columns with all of my associated data are gone. I brought the shapefile into QGIS, and see that the variable distances buffers were created as a wanted, but that these buffers were missing all of the data I wanted them to contain.
How can I fix my code so that I create these variable distance buffers based on my "Distances" column, but so that all of the attribute data within each of the original polygons is preserved within its newly buffered polygon?
.apply()
on my original gdf, would that look like:polygons_buffered = polygons.apply(buffer(distances))
? I am just a bit confused if I would be "applying" the.buffer(distances)
part to the original gdf or if I wrote that correctly.polygons.apply(buffer(distances))
and it did not work for me.