3

With arcpy, I want to calculate the mean value of a specific field in a shapefile and print the resulting value for the user. I use the Summary Statistics tool and it works, but the resulting value is stored in a table at a specific path, but how can I access the calculated value with python?

Here is my code. It will only output the path to the table, but I need the calculated value.

stats = arcpy.Statistics_analysis(myshapefile, "G:/temp/stats", [[field, "MEAN"]])
arcpy.AddMessage(stats.getOutput(0))
2

The way I would recommend to access the contents of a table is by using arcpy.da.SearchCursor() to read it:

SearchCursor establishes read-only access to the records returned from a feature class or table.

Returns an iterator of tuples. The order of values in the tuple matches the order of fields specified by the field_names argument

The asker also:

found a useful function on this website: https://arcpy.wordpress.com/2012/02/01/calculate-a-mean-value-from-a-field/

which groups the Summary Statistics with a Search Cursor to read its output and return the Mean value from a field.

  • 1
    I tried that and it worked out - thanks a lot! I also found a useful function on this website: arcpy.wordpress.com/2012/02/01/… – the_chimp Mar 15 '16 at 7:46
  • Can you please include how the code works on the real data? I am not sure, how exactly use "def" in my script. Is the "na" object where my mean value is stored? Thank you! – maycca Jan 12 '18 at 16:52
  • @maycca my answer makes no mention of "def" or "na" so it sounds like you need to ask a new question to let us know precisely what you want to do, what you have tried, and where you are stuck. – PolyGeo Jan 13 '18 at 0:27

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.