2

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.

A pic of my attribute table

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)
5

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.