1

I use the following logic in ModelBuilder to get a result as seen below:

A feature class has a Column with the name "Value" which is passed through to the Summary Statistics Tools which calculates the Mean of the Column "Value".

The Get value tool then retrieves this from the summary table created and passes it to the calculate field tool. This one value is then added to all the row in the column named Mean.

Model Builder Logic

Attribute Table

arcpy.Statistics_analysis(outPRJ, r"C:/Users/dean/Desktop/Script Dependencies/03_Output/4_Stats/minValue.dbf",
                          [["Value", "MIN"]])

tableMin = r"C:/Users/dean/Desktop/Script Dependencies/03_Output/4_Stats/minValue.dbf"
fieldMin = ["MIN_Value"]
with arcpy.SearchCursor(tableMin, fieldMin) as cursor:
    for row in cursor:
        MinValue = print("MIN_Value: {}")


arcpy.CalculateFields_management(outPRJ, "Min", MinValue, "PYTHON_9.3")

I want to use the same logic in a python script by using a search cursor and tried the code below which gives me an error. The Error received is the following:

Traceback (most recent call last):
  File "D:/Dropbox Data/Dropbox/02_Agrimotion/ZZZ_Scripts/Chem Soil Interpolation - Updated Header Names.py", line 189, in <module>
    with arcpy.SearchCursor(tableMin, fieldMin) as cursor:
AttributeError: __enter__
  • What is the error, including line number, that you receive when running the code that you present? – PolyGeo Apr 4 at 14:19
  • 2
    The field Min_Value doesn't exist in what I presume is outPRJ per your screenshot above, so CalculateField_management won't be able to pass that. You likely need to perform a join and identify the fully qualified field name from the join, although it's unclear from the description and screenshot which field you'd be joining on. Please edit your question and images. – smiller Apr 4 at 18:08
  • OK, I think I have a better understanding now. You want to populate the minimum value as calculated already in the Statistics Analysis which is based on one single column and does not vary based on another field. You want this same value to be added to every single row. You have already added a variable MinValue which holds this value from the (last row within the) minValue table. Instead of using !Min_Value! (a field name from a different table) in your calculate field, use MinValue (the variable created from SearchCursor code). @PolyGeo if you reopen I can add this as an answer. – smiller Apr 5 at 13:57
  • The ERROR 000800 makes me think you're using Pro (Python3) not Desktop. Although the option for "PYTHON_9.3" is supposed to be supported for backwards compatibility per their documentation, you can switch this to be "PYTHON3" or leave the option blank (Python is default in the version of arcpy that comes with ArcPro.) – smiller Apr 5 at 14:03
  • 1
    Again I recommend not using the non-data access module version of SearchCursor, because it is no longer supported and has been replaced by da.SearchCursor. Note that the error you receive now under SearchCursor is due to not having the expected parameters -- in SearchCursor fields is the 4th parameter, or you can pass as a named parameter. – smiller Apr 5 at 18:37
2

Use da.SearchCursor to store all values in a list. Then use pythons built in functions to find min, max and mean (sum and len(gth)). Write values to the attribute table using da.UpdateCursor.

import arcpy

fc = r'C:\database.fgdb\featureclass' #Change
valuefield = 'Value' #Change
new_fields = ['min','max','mean'] #Change

for field in new_fields:
    arcpy.AddField_management(in_table=fc, field_name=field, field_type='DOUBLE')

all_values = [i[0] for i in arcpy.da.SearchCursor(fc,valuefield)]

minval = min(all_values)
maxval = max(all_values)
meanval = sum(all_values)/len(all_values)

with arcpy.da.UpdateCursor(fc, new_fields) as cursor:
    for row in cursor:
        row[0]=minval
        row[1]=maxval
        row[2]=meanval
        cursor.updateRow(row)
1

You may have a better experience with UpdateCursor as outlined by BERA, but to answer your initial question using CalculateField:

arcpy.Statistics_analysis(outPRJ, r"C:/Users/dean/Desktop/Script Dependencies/03_Output/4_Stats/minValue.dbf",
                          [["Value", "MIN"]])

tableMin = r"C:/Users/dean/Desktop/Script Dependencies/03_Output/4_Stats/minValue.dbf"
fieldMin = ["MIN_Value"]
with arcpy.da.SearchCursor(tableMin, [fieldMin]) as cursor:
    for row in cursor:
        MinValue = row[0] 


arcpy.CalculateField_management(outPRJ, "Min", MinValue, "PYTHON_9.3")

A few notes:

  1. I recommend not using the non-data access module version of SearchCursor, because it is no longer supported and has been replaced by da.SearchCursor. Note that the error you receive now under SearchCursor is due to not having the expected parameters -- in SearchCursor fields is the 4th parameter, or you can pass as a named parameter, e.g. arcpy.SearchCursor(tableMin, fields=fieldMin). See http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/searchcursor.htm for more information on this (old) tool.
  2. If CalculateField_management (note: changed from your code which had CalculateFields - plural) does not work, double-check the error and use either "PYTHON", "PYTHON_9.3" per http://desktop.arcgis.com/en/arcmap/10.6/tools/data-management-toolbox/calculate-field.htm
  3. I assume that the variable MinValue will be able to pass due to code examples here: http://desktop.arcgis.com/en/arcmap/10.6/tools/data-management-toolbox/calculate-field-examples.htm. If not, another way is to join the tables and reference by column name.

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.