I would like to change one field values into negative (i.e. original column, row 1 = 5, new column row 1 = -5 in a feature class list.

I got until the addfield and calculate field functions, but it is not clear how to do the negative transformation.

ARCGIS help page has some instructions about negative expressions

-x, negative expression of x, x = 5 --> -x, -5

How to use it with arcpy is not very clear. It seems that an expression should be added either at the '!original_value!' or as a code block after the expression type (python 9.3).

for fc in arcpy.ListFeatureClasses():  
    #output = path.join(outWS, fc)
    #"GEODESIC" calculates distances in meters!
    #arcpy.analysis.Near(fc, idFeat, "", "NO_LOCATION", "NO_ANGLE","GEODESIC") 
    arcpy.AddField_management(fc, 'location_value', "FLOAT")
    arcpy.CalculateField_management(fc, 'location_value', '!original_value!', "PYTHON_9.3", "")
  • hi, I tried this one: arcpy.CalculateField_management(fc, 'location_value', '!original_value!-', "PYTHON_9.3",""), but I only got and error msg - 000539: SyntaxError: unexpected EOF while parsing (<expression>, line 1) Failed to execute (CalculateField). – Tre Sep 24 '15 at 9:41

A very simple technique to make numbers negative is to multiply them by -1.

In an ArcGIS environment using the CalculateField tool the syntax would be:

arcpy.CalculateField_management(fc, 'location_value', "!original_value! * -1", "PYTHON_9.3", "")
  • 3
    just a comment, but if your input value is negative, then you'll make it positive. "-1*abs(!original_value!) " would be 'safer' – radouxju Sep 24 '15 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.