Calculate the mean of values in several fields in Field Calculate

5681
1
Jump to solution
02-03-2015 11:43 AM
AaronKing
New Contributor II

Lets say i have a feature class or table with 3 fields

[val1], [val2], [val3]

all are floating point fields.

i add a new field

[mean]

and I want to use Field Calculate to get

[mean] = AVERAGE([val1], [val2], [val3])

 

how do i do that?  I have tried AVERAGE, AVG, MEAN, I have tried looking for help on this. Usually, VBA commands as used in Excel or Access work fine in field calculator, but AVERAGE does not work. 

 

I dont care if its python or VBA, I just need something that works in Field Calculate.

 

I have no null values in any of the 3 fields I am trying to average.

 

NOTE: I am NOT trying to find the average of values in a field (vertical) - right clicking the field name and selecting statistics will not do what i need, nor will the Summary Statistics tool.

 

Thanks,

0 Kudos
1 Solution

Accepted Solutions
AnthonyGiles
Frequent Contributor

Aaron,

Here is a solution using python, this will account for any null values:

def mean(val1, val2, val3):

  fieldList = [val1, val2, val3]

  validList = []

  for i in fieldList:

    if i != None:

      validList.append(i)

  meanVal = sum(validList)/len(validList)

return meanVal

or if you are sure there is no null values a simple sum([!val1!, !val2!, !val3!])/3 should work

View solution in original post

1 Reply
AnthonyGiles
Frequent Contributor

Aaron,

Here is a solution using python, this will account for any null values:

def mean(val1, val2, val3):

  fieldList = [val1, val2, val3]

  validList = []

  for i in fieldList:

    if i != None:

      validList.append(i)

  meanVal = sum(validList)/len(validList)

return meanVal

or if you are sure there is no null values a simple sum([!val1!, !val2!, !val3!])/3 should work