Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need some assistance with the syntax for a CalculateField_management calculation as I have still yet to master all the python syntax rules.

What I am trying to do is find the max value in a variable number of fields in order to populate another field. I am using the ListFields function to discover the desired fields to choose from, but getting that list into the formula is giving me some difficulty.

import arcpy, os, string
LAYERS = arcpy.GetParameterAsText(0)
SLOSHFILEDS = [f.name for f in arcpy.ListFields(LAYERS,"","DOUBLE")
arcpy.CalculateField_management (LAYERS, "MAXSURGE", max(SLOSHFILEDS))

I have tried any number of different string combinations for the max() calc to no avail (not that this particular variation shows that).

Adding/Changing the following to the script doesn't give me the syntax error that I would recieve with the above, but it does give me a "The calculate value is invalid for the row with ObjectID = 0..." x 18,526 (or however many rows are in my table) and then does nothing to the table except populate my MAXSURGE field with 0's.

SLOSHFILEDS = arcpy.ListFields(LAYERS,"","DOUBLE")
fieldNameList = []
for field in SLOSHFILEDS:
        if not field.required:
            fieldNameList.append(field.name)
arcpy.CalculateField_management (LAYERS, "MAXSURGE", max(fieldNameList))

Hard coding the field names into the formula works great, but of course, I will not always have the same number of fields or same field names to work with.

Any help would be appreciated.

-John

share|improve this question

2 Answers 2

The first step is to incorporate conditional logic (e.g. if statement) within your loop to look for the field name that you want to run your calculation on.

e.g.

fieldName = arcpy.GetParameterAsText(0)

for field in SLOSHFILEDS:
    if field.name == fieldName: 
        arcpy.CalculateField_management.....
share|improve this answer
    
I had thought of that, but I didn't want to hard code field names into the script. The above code is a snippet of a longer code that unions any number of storm surge grids that may appear during a hurricane event. The field names will not be the same every time. The problem I am trying to solve is to be able find the max storm surge values for a given grid cell after a storm event has passed and apply it to a single grid that I can then perform analysis on. –  NCFMP Aug 29 '12 at 12:39
    
@NCFMP, you can make your field name a user parameter for your script, and search for that variable in your if statement (see updated code). Here is a link from a previous question from this site that deals with finding max value within a field, gis.stackexchange.com/questions/25918/…. –  artwork21 Aug 29 '12 at 13:33
    
artwork21, thanks for your suggestion, but user parameter is not an option. I may have two fields to grab the max value per row from (I should have been more clear) or I may have 50 for 18000+ rows, it all depends on the storm. It has been suggested that I use search cursor, but cursors always give me trouble. The bottom code will run only it gives me the "The calculate value is invalid for the row" error. Converting the list to a string gives me the "The value type is incompatible with the field type" error, yet if I type aout the individual fields in the max() calc it works perfectly. –  NCFMP Aug 29 '12 at 14:53
    
I guess my question is: Is there a way to make python "think" that it is getting max(!F1!, !F2!, !F3!) when all the field values are contained in a list object? (e.g max(list)) –  NCFMP Aug 29 '12 at 14:54
    
Thank you for your suggestions. I figured out my problem. In order to the convert the list object into a usable string, I needed to use the .join function and then concatenate the formula. SEE EDITS ABOVE. –  NCFMP Aug 29 '12 at 16:30

I figured out the problem (with help, of course). I needed to convert the list object to a string using the .join function.

import arcpy, os, string
LAYERS = arcpy.GetParameterAsText(0)
SLOSHFILEDS = arcpy.ListFields(LAYERS,"","DOUBLE")
fieldNameList = []
for field in SLOSHFILEDS:
        if not field.required:
            fieldNameList.append(field.name)
SLOSHSTRING = "!" + "!, !".join(fieldNameList) + "!"
calculation = "max(" + SLOSHSTRING + ")"
arcpy.CalculateField_management (LAYERS, "MAXSURGE", calculation, "PYTHON", "")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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