How to find Max value among Fields

16376
8
Jump to solution
03-13-2017 09:32 AM
StephenCoppola
Occasional Contributor

Good Day GeoNet Community,

Would like to inquire how to search among rows of a table and find the maximum value only using a selected set of fields.  For example, If the table consists of five columns ID,F1,F2,F3,F4, would want to search and find the maximum value in F2 and F4.  The maximum value would become an value in a new field, called MX.  An arcpy solution is optimal and generic case is best.

Your suggestions are much appreciated, thank you for your time.

Example Table ("MyVals")

IDF1F2F3F4MX
11221333
232655
369566
4778877
599988
6477199
822441233
975331
109452
Tags (2)
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

If you want the max of F2 and F4, in field calculator:

max(!F2!,!F4!)

Or updateCursor (untested):

with arcpy.da.UpdateCursor(fc,['F2','F4','MX']) as cursor:
  for row in cursor:
    row[2] = max(row[0],row[1])
    cursor.updateRow(row)

View solution in original post

8 Replies
DarrenWiens2
MVP Honored Contributor

If you want the max of F2 and F4, in field calculator:

max(!F2!,!F4!)

Or updateCursor (untested):

with arcpy.da.UpdateCursor(fc,['F2','F4','MX']) as cursor:
  for row in cursor:
    row[2] = max(row[0],row[1])
    cursor.updateRow(row)
TokiLee
Emerging Contributor

Hi Darren,

If I have 300 fields, if there a way to loop through all fields to find the max without list all fields manually?

Thank you!

0 Kudos
RosialineMarques_Roedel
Emerging Contributor

Hi 

I'm trying to apply max function but it's not working...any relation with properties??? string value?

0 Kudos
Waffle_House
Frequent Contributor

I agree this doesn't seem to work and therefore should not be marked as the solution. @DarrenWiens2 

This thread seems to agree: https://gis.stackexchange.com/questions/90697/how-to-populate-new-field-using-expression-containing-...

 

 

0 Kudos
DarrenWiens2
MVP Honored Contributor

What doesn't work? Quick test seems successful:

Screen Shot 2021-03-09 at 5.00.36 PM.png

0 Kudos
DarrenWiens2
MVP Honored Contributor

 Python cursor solution is successful, too:

Screen Shot 2021-03-09 at 5.08.12 PM.png

 

0 Kudos
KedaravindanBhaskar
Regular Contributor

Hello Stephen,

I faced a similar issue. The following code worked for me -

Max($Field1,$Field2)

Expression type - Arcade.

Regards, Kedar


Bud
by
Esteemed Contributor
0 Kudos