# ArcGIS do calcuation on multiple input fields of attribute table and the number of input fields is changing

I have attribute table with headers: ID(bgrp), F1 (F2011_46), F2 (F2011_48), ...Fn

I want to do math calculations on the fields from F1 to Fn. For example, count the total number of non-zero Fn fields for each ID unit. Normally, a field calculator using python script will do the job. But the problem is n vary each time and I have many such tables, so I don't know if I should modify the input fields each time when do field calculation for one such table. Additionally, n is usually large, 30 to 100, and I don't think it is efficient to manually select all the Fn fields in a Pre-Logic Script Code.

• I take it that it's ArcGis? You should probably include a tag or reference in your question indicating the software. This is probably not something you want to do in Field Calculator but if you persist then you could conceivably use ListFields to get the fields... what field type in particular? Do you want all the numeric, double, text..? Mar 17, 2015 at 22:27

## 1 Answer

Here is an answer, but it's a bit hacky... halfway between Field Calculator and true arcpy tool:

``````def CalcThis(YourID):
YourFeatClassOrLayerName = "Polylines" # Change this to your layer name
desc = arcpy.Describe(YourFeatClassOrLayerName)

fList = arcpy.ListFields(YourFeatClassOrLayerName,"L*") # List of matching fields, in this case fields starting with L
fRange = range(len(fList))
sumNonNull = 0
# field names into list of string
fNameList = []
for f in fList:
fNameList.append(f.name)

with arcpy.da.SearchCursor(YourFeatClassOrLayerName,fNameList,desc.OIDfieldName + " = " + str(YourID)) as sCur:
for sRow in sCur:
for thisIndex in fRange:
if sRow[thisIndex] != None:
sumNonNull += 1

return sumNonNull
``````

This script gets a list of matching fields, the OIDfieldName for the feature class (important later) then turns the field list into a list of strings for arcpy.da.SearchCursor - with a query on just one row! then increments if the field is not None (NULL) and returns that. Considering how sloppy it is it's still reasonably fast.