Search code examples
pythongdalshapefilearcpyogr

How to replace values in an attribute table for one column?


I need to replace values in an attribute table for one column (replace zeroes in column named "label" to 100). Is this possible using ogr or python? I have to do this for 500+ shapefiles.


Solution

  • In the Esri ArcGIS realm, Update Cursors are typically used for this type of operation.

    For example

    import arcpy
    
    # Your input feature class
    fc = r'C:\path\to\your.gdb\feature_class'
    
    # Start an update cursor and change values from 0 to 100 in a field called "your_field"
    with arcpy.da.UpdateCursor(fc, "your_field") as cursor:
        for row in cursor:
            if row[0] == 0:
                row[0] = 100
            cursor.updateRow(row)