Deleting field/column of shapefile with OGR/GDAL Python?
You can also use OGR SQL ALTER TABLE
statement to DROP
the column directly via ExecuteSQL()
:
from osgeo import gdal
ds = gdal.OpenEx("my_shp.shp", gdal.OF_VECTOR | gdal.OF_UPDATE)
ds.ExecuteSQL("ALTER TABLE my_shp DROP COLUMN my_field")
I usually see this done by creating a copy of the shapefile, but specifying which fields to include in the copy. Once a copy is made, it's easy enough to delete the original file and rename the copy.
Here is a simple non-python way. Otherwise, I think you should be able to create a solution based on this python example.
To delete a column from the attribute table of a shapefile, I used this:
dataSource = driver.Open("MyShapefile.shp", 1)
layer = dataSource.GetLayer()
layer.DeleteField(4)
where 4 is the index of the column I want to delete (starting from 0).
I hope it was what you asked.