Question

I have a .gdb and 1 feature class called waterlevelDifference. There are two columns called Label (text field) and Difference (double field). The Difference column contains numbers while the Label column is empty and waiting to be populated.

What I want to do, is select attributes from Difference (eg Difference > 0.30) and then populate the Label (eg Label = "Greater than 0.30") based on the selection. Initially I was going to use arcpy.SelectLayerByAttribute_management (to select attributes) and then use arcpy.CalculateField_management (to populate), but you can't use arcpy.SelectLayerByAttribute_management on a feature class.

My question is, what are the other ways I can select and populate attributes using a feature class?

Was it helpful?

Solution

The easiest way to do this is using a python code block in the field calculator in ArcMap, however if you're wanting to do it in a script, I would create an arcpy.UpdateCursor(), then calculate the value for Label and add it row by row.

feature_class = r"path\to\class"
cursor = arcpy.UpdateCursor(feature_class)
for row in cursor:
  label = ""
  difference = row.getValue('Difference')
  if difference == 0:
    label += "Label is 0"
  elif difference > 0.30:
    // ...
  row.setValue("Label", label)
  cursor.updateRow(row)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top