# assign a number for each group in field calculator

I have several thousand features each of which has a name. The names are not unique. what I want to do with the field calculator is assign a number to each name so I can refer to a number and always get the same name.

``````chris, dave, chris, tom, dave, chris , dave, tom
``````

I'd like my number column to show

``````1, 2, 1, 3, 2, 1, 2, 3
``````

Is this something I can do using python in field calucator?

• Is your list always going to be table dependent (each table/layer may have a slightly different list)? Aug 4, 2014 at 14:23
• this option is very interesting. Can we us the "Codeblock" if- elif calculation in a stand alone arcpy script ? Apr 22, 2016 at 14:15
• This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review Apr 22, 2016 at 14:41
• This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review Apr 22, 2016 at 16:30

If you know all of the possible input values in advance, it is possible to do this with Field Calculator by turning on the "Codeblock" and creating a function like the following:

``````def classify(name):
if name == 'chris':
return 1
elif name == 'dave':
return 2
elif name == 'tom':
return 3
else:
return 0
``````

And then the expression you would use to calculate another numeric field using that function would be:

``````classify(!NAME_FIELD!)
``````

If you don't know all of the possible values in advance, or if there are so many that it would be ridiculous to type an `elif` statement for each one, you'd probably have to write a custom Python script to do this for you instead of using Field Calculator.

Edit: As discussed in the comments, if you don't care about the specific numeric value assigned to each unique text value, this can probably be done with a global list variable in the codeblock, with the following code (based on your comment):

``````mylist = []
def classifier(value):
global mylist
if value not in mylist:
mylist.append(value)
return mylist.index(value)
``````
• `calculate(!NAME_FIELD!)` or `classify(!NAME_FIELD!)`? Aug 4, 2014 at 14:08
• My mistake; haven't had enough coffee yet... Aug 4, 2014 at 14:08
• thanks, I don't have the list of names so it looks like I'll need a separate script. Do you think I could do it in the codeblock with by adding each name to a list then if the next name features in the list getting the index position? Aug 4, 2014 at 14:19
• It may work if you create a global variable, similar to the code in this answer: gis.stackexchange.com/questions/67916/… Aug 4, 2014 at 14:32
• Am I barking up the wrong tree with this then? mylist = [] def classifier(value): if value in mylist: mylist.append(value) return mylist.index(value) else: mylist.append(value) return mylist.index(value) Aug 4, 2014 at 16:00

What about looping through, throwing the names in a dictionary as keys? Use a counter to add the value if the key doesn't exist, otherwise use the value from the key. So 'Chris' doesn't exist when first encountered; 'Chris' becomes the key and 1 the value. Then 'Dave is next, doesn't exist, so becomes a new key with 2 as the value. Then 'Chris' is encountered again, but Chris already exists as a key, so that value, 1, is used, and so on.