Calculate field using python to get most visited facility by area

694
6
Jump to solution
11-29-2019 08:47 AM
CoreyStegall
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!

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Corey Stegall ,

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 

View solution in original post

6 Replies
XanderBakker
Esri Esteemed Contributor

Hi corey.stegall ,

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
CoreyStegall
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.   

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Corey Stegall ,

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 

CoreyStegall
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. 

0 Kudos
RandyBurton
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‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
CoreyStegall
New Contributor

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

0 Kudos