# Calculate field using python to get most visited facility by area

762
6
11-29-2019 08:47 AM
New Contributor

Hello Everyone,

In arcgis pro, I have a feature class with an attribute table that contains fields with our facility names and the rows are census tracts.  The values are the number of visitors for each facility in each tract

Facility1   Facility2   Facility3   etc            MostVisitedFacility <--Desired result field

Tract 1   1000         3000         2000                         Facility2

Tract 2   2000         1000         4000                         Facility3

.

One of my tasks is to determine the most visited facility in each tract.

I can do this in Excel but I would like to work within ArcGIS Pro since the table is already in there.  Plus it would save me at least 5-10 minutes for every one of these I do.

The way I do it in Excel is to use the max() function on each tract.  Then I use the match() function to determine the column number of the most visited facility.  Then I use the index() function to return the first row value of the column which is the facility name.

What is the python equivalent?  In my attempt to figure it out myself, I stumbled upon the SearchCursor function and ListFields function but I'm lost as to how to apply it or even if those are the appropriate functions.  Can someone point me in the right direction?

Thanks so much!

1 Solution

Accepted Solutions
Esri Esteemed Contributor

Sorry for that. Indeed to use this in a field calculation you will need to use a different approach:

However, it might be better to use the python window as suggested by rvburton

6 Replies
Esri Esteemed Contributor

I guess you can do something like this:

``````# create a dictionary with descriptions andd values
facilities = {"Facility1": !Facility1!,
"Facility2": !Facility2!,
"Facility3": !Facility3!}

# get description for maximum value and return it
facility = [k for k, v in facilities.items() if v == max(facilities.values())][0]
return facility
‍‍‍‍‍‍‍‍``````
New Contributor

Thanks Xander!  I replaced the field names with numbers and it works great.  I'm getting syntax errors when I put the field names back in the dictionary.

Esri Esteemed Contributor

Sorry for that. Indeed to use this in a field calculation you will need to use a different approach:

However, it might be better to use the python window as suggested by rvburton

New Contributor

That worked great!  Thanks again to everyone.  I'm going to try it the other ways with the python window as well for practice.

MVP Alum

Another way:

``````fc = 'feature'
fields = ['Facility1', 'Facility2', 'Facility3', 'MostVisited'] # field names

facility = ['One', 'Two', 'Three' ] # facility names

with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
row[3] = facility[row[:3].index(max(row[:3]))]
# row[3] is the 'MostVisited' field
# row[:3] are the first 3 fields (visit counts)
cursor.updateRow(row) # this would do the update‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
New Contributor

Thank you Randy!  I'll try this one as well.  You are all great help.