4

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.

For example if I had:

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?

4

2 Answers 2

4

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)
7
  • 2
    calculate(!NAME_FIELD!) or classify(!NAME_FIELD!)?
    – Erica
    Aug 4, 2014 at 14:08
  • My mistake; haven't had enough coffee yet...
    – nmpeterson
    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/…
    – nmpeterson
    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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.