[GIS] Using ArcPy to multiply fields in attribute table

arcpyfield-calculatorfields-attributespython-parser

I am trying to identify a method to multiply 2 fields in the same attribute table and write the product of the calculation into an added field. The code creates the empty fields, now I just want to understand how to use arcpy to run a field calculation.

This is what I have so far:

# Set the local parameters
inFeatures = "ESR_States"
joinField = "STID"
joinTable = "ESR_Weather"
# Join two feature classes by the STID field 
arcpy.JoinField_management (inFeatures, joinField, joinTable, joinField)
# Add fields to ESR_States
fieldName1 = "POT_PROD"
fieldName2 = "POT_CONSU"
fieldName3 = "SELF_SUS"
fieldName4 = "FUTURE_POP"
arcpy.AddField_management(inFeatures, fieldName1, "LONG")
arcpy.AddField_management(inFeatures, fieldName2, "LONG")
arcpy.AddField_management(inFeatures, fieldName3, "LONG")
arcpy.AddField_management(inFeatures, fieldName4, "LONG")
arcpy.CalculateField_management()

Best Answer

I think the easiest way to learn "how to use ArcPy to run a field calculation" is to:

  • Go to the Search window and find the Calculate Field tool
  • Open its tool dialog
  • Configure a test calculation and run the tool
  • Open the Geoprocessing | Results window
  • Right-click on the Calculate Field run that you see there
  • Choose Copy As Python Snippet
  • Paste the code snippet into your script
  • Modify the code thus pasted, if necessary

If you need more help with using the Field Calculator from ArcPy than this then I recommend reviewing our self-assembling FAQ on ArcPy and the Field Calculator.

Related Question