Python Expression to Change Field Labels

3318
9
08-13-2014 10:59 AM
ShawnGazzano
New Contributor II

How is the Python expression written (i.e. what is the syntax?) to change a field's labels from its unique values to custom labels?

The field I'm working with is DISTRICT.  Its unique values are 1, 2, 3, 4, and 5.

On my map, I want the districts to be labeled with their supervisors' names, instead of with their numbers.

0 Kudos
9 Replies
IanMurray
Frequent Contributor

It is pretty simple.

def FindLabel ( [DISTRICT] 😞

  if DISTRICT == 1:

    return "Bob Hope" #Input your supervisor name there

  elif DISTRICT == 2:

    return "Bob Costas" #Input your Supervisor name there

  elif DISTRICT == 3:

    return "Bob Marley" #Input your Supervisor name there

  elif DISTRICT == 4:

    return "Bobby Bowden" #Input your Supervisor name there

  elif DISTRICT == 5:

    return "Bob the Clown" #Input your Supervisor name there

Similar to that, just use if/elif statements to evaluate the value of District and return the name you want.

Use Python for parser and advanced expression checked.

0 Kudos
ShawnGazzano
New Contributor II

I entered

def FindLabel ( [District] 😞  

  if District == 1:  

   return "Bob Hope"

but the response to clicking the Verify button was "Carriage returns are not allowed in simple expressions."

I took out the extra "=" in front of "1", but the Verify response was the same.

0 Kudos
IanMurray
Frequent Contributor

Like I said, make sure the advanced box is checked.LabelExpression.png

It doesn't allow multiple lines of code for simple expressions, so the advanced box needs to be clicked

0 Kudos
ShawnGazzano
New Contributor II

Ok, I've got Advanced checked and Python for parser.  That validates the expression.

But in the Expression Verification window, the Sample Text String and Sample Label are blank.

When I press OK to get out of Expression Verification / Label Expression / Layer Properties windows, my map updates, but without labels; they are all blank.

The expression I have entered is:

def FindLabel ( [DISTRICT] 😞  

  if [DISTRICT] == 1:  

   return "Bob Hope"

0 Kudos
IanMurray
Frequent Contributor

can you check to see if DISTRICT is a text field(string type) or a numeric type?

If text, make it if int([DISTRICT) == 1:

XanderBakker
Esri Esteemed Contributor

That's nice, I was't aware of this possibility.

If the list get a little longer, you may want to consider using a dictionary, like this:

def FindLabel([YourFieldName]):

    dct = {1: "Bob Hope", 2: "Bob Costas", 3: "Bob Marley",

          4: "Bobby Bowden", 5:"Bob the Clown"}

    district = int([YourFieldName])

    if district in dct:

        return dct[district]

    else:

        return "<ITA>Unknow supervisor</ITA>"

I used this on the feature ID (FID) field of a shapefile with values ranging from 0 to 7 and got this:

LabelExpression.png

ShawnGazzano
New Contributor II


It's a text field.

def FindLabel ( [DISTRICT] 😞  

  if int( [DISTRICT] ) == 1:  

   return "Bob Hope"

still returns blank labels.

0 Kudos
IanMurray
Frequent Contributor

Hmm, mine works just fine on a sample dataset.  Did you make sure that you turned labels on for that layer?  Also, python is case sensitive, so make sure that your field is syntactically correct(proper upper/lower case, spaces,etc. included.

0 Kudos
ShawnGazzano
New Contributor II

Yes, the layer's labels are turned on, and the expression appears to be syntactically correct.

I also tried Xander's suggestion:

def FindLabel([DISTRICT]):

dct = {1: "Bob Hope", 2: "Bob Costas", 3: "Bob Marley", 4: "Bobby Bowden", 5:"Bob the Clown"}

district = int([DISTRICT])

but same result: expression is validated, but with blank labels.  I'll continue to test....

0 Kudos