# Using Python in field calculator to extract highest value from 3 columns [closed]

I have three columns, one for percentage White, percentage Hispanic, and percentage African American per census tract. I'm trying to use Python to extract the population subtype with the highest percentage into a new column called "highest_percentage". The highest percentage category will have 3 classes: 1,2, and 3. I can not figure out how to set a conditional statement so that I can automatically assign a value of 1,2, or 3 to the census tract to use in a classified map.

I was able to write code to find the highest percentage and assign the new field the same value as that percentage, but i'm trying to find which field has the highest value and assign it 1-3 based on a classification. 3 white, 2 Hispanic, 1 African American

I've attached a screenshot of the table and the code window below.

• Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please always present any code using formatted text (highlight it use the {} button) rather than pictures.
– PolyGeo
Commented Feb 14, 2019 at 5:29

I prefer to use an arcpy UpdateCursor for tasks such as these, mainly because I think the syntax is easier to work with and can be incorporated into larger workflows.

In your case, find the index of the largest percentage in each row. For example:

import arcpy

fc = r'C:\path\to\your\file_geodatabase.gdb\featureclass'

with arcpy.da.UpdateCursor(fc, ("african_american", "hispanic", "white", "index")) as cursor:
for row in cursor:
max_val = max(row) # Find the max value in each row
max_index = row.index(max_val) # Find the index of the max value
row[3] = max_index + 1 # Apply a correction on the index
cursor.updateRow(row)


Try this

Pre-Logic Script Code:

def maxnum(fields):
max_value=fields[0]
return_value=1
if (fields[1]>max_value):
max_value=fields[1]
return_value=2
if (fields[2]>max_value):
max_value=fields[2]
return_value=3
return return_value


trav_tracts.highest_percent=

maxnum([!trac_tracts.percent_aa!,!trav_tracts.percent_hispanic!,!trac_tracts.percent_white!])