# Using ArcPy to multiply fields in attribute table?

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)
fieldName1 = "POT_PROD"
fieldName2 = "POT_CONSU"
fieldName3 = "SELF_SUS"
fieldName4 = "FUTURE_POP"
arcpy.CalculateField_management()


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.

I rarely use the Calculate Field tool to alter attributes in Python, but rather an Update Cursor as I find the syntax easier and the Python cleaner. Note that a cursor operates on a row by row basis. Here is an example of how you would multiply two fields and write the product to a third field. This script assumes you have already created the fields "field1", "field2" and "field3".

import arcpy

fc = r'C:\path\to\your.gdb\featureclass'

with arcpy.da.UpdateCursor(fc, ["field1", "field2", "field3"]) as cursor:
for row in cursor:
row[2] = row[0] * row[1] # This means field3 = field1 * field2
cursor.updateRow(row)