# Calculating mean value of 280 fields with Field Calculator from attribute table of shapefile?

I have to average 280 fields in 1 field. Each field has 300 rows.

That is my problem. I think I need a python script. I don't want to create a raster for each field and then use the raster calculator in ModelBuilder with iterations...

I don't know how to make that script in Python.

In this pic, the field "Prom_P2" is what I need to fill with the mean or average of the previous 280 fields.

This is code that @BERA shared with me, and it works:

import arcpy
shapefile = r'D:\PYTHON_practica\pp_1998_2015.shp'

field_names=[f.name for f in  arcpy.ListFields(shapefile,wild_card='*Lay*')] #Create a list of all the fields with 'Lay' in field name
field_names.append('promedio') #add your mean field to the list

with arcpy.da.UpdateCursor(shapefile,field_names) as cursor:
for row in cursor:
row[-1]=sum(row[:-2])/len(row[:-2]) #Calculate mean and assign this to the mean field (-1)
cursor.updateRow(row)


You can execute the code below in the Python window of ArcMap. I'm using the UpdateCursor which is like a more powerful Field Calculator. I assume all fields from which the mean is calculated have 'Lay' in field name.

import arcpy
shapefile = r'C:\path\to\shapefile\shapefile.shp'

field_names=[f.name for f in arcpy.ListFields(shapefile,wild_card='*Lay*')] #Create a list of all the fields with 'Lay' in field name
field_names.append('Prom_P2') #add your mean field to the list

with arcpy.da.UpdateCursor(shapefile,field_names) as cursor:
for row in cursor:
row[-1]=sum(row[:-2])/len(row[:-2]) #Calculate mean and assign this to the mean field (-1)
cursor.updateRow(row)