2

I have a table with a whole bunch of census variables (race, age, education, etc). I want to multiply all of these fields by a factor that I calculated. For example:

Field1 has values of 10,20,30,40

Field2 has values of 120,340,230,210

Field3 has values of 500,3402,0342,9402

Multiplier field has values of 0.1, 0.2, 0.3, 0.4

I want to 'iterate' through all the columns and multiply them by my multiplier field. Traditionally, I would use field calculator and do !Field1! * !Multiplier!, but I am trying to avoid having to do this for 200+ fields.

Any suggestions?

I am using ArcMap 10.2.2.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Bwize
  • 23
  • 4
  • I think the first thing you will want to do is get a list of the fields you want to iterate through. Then, once you have that list, a pretty simple python script can be put together to iterate through your list, and calculate your values using the calculate field tool. – evv_gis Apr 01 '15 at 21:16
  • Where do you want to store this calcs? What do you expect to get? Single number per column? – FelixIP Apr 01 '15 at 21:18
  • Related: http://gis.stackexchange.com/questions/54771/model-builder-iterate-through-columns-in-a-table – Chris W Apr 01 '15 at 22:02

1 Answers1

2

If I understand correctly, each row has a value in the "Multiplier" field, by which you'd like to multiply the values in all of the other fields.

Field calculator only does one field at a time, though you could run it in batch mode. For 200+ fields, I'd recommend some code like the following which uses a search cursor. Put it in the python console.

layer = 'layername' #drag from TOC
multfield = "multiplier field name"

fieldnames = [i.name for i in arcpy.ListFields(layer) if not i.required]
ind = fieldnames.index(multfield) #now you know where it is in the list

with arcpy.da.UpdateCursor(layer,fieldnames) as cursor:
    for row in cursor:
        for n in range(len(fieldnames)):
            if n == ind:
                continue #don't update the multiplier field
            row[n] = row[n]*row[ind]
        cursor.updateRow(row)

This is assuming that you want to multiply every field by the multiplier, besides the multiplier itself, and any automatic fields like "OBJECTID". If you have text fields, you'll get an error.

mr.adam
  • 3,224
  • 14
  • 26