[GIS] How to label only the odd or even integers of a field using python in the label expression in arcmap

arcgis-desktoparcmapexpressionlabelingpython-parser

I'm new to python and would like to use the label expression box with python to display only the odd or even integers from the field, [Frame_Number].

I was thinking something like

def FindLabel ( [Frame_Number] ):
  for num in [Frame_Number]:
     return num % 2
  return [Frame_Number] 

But I know that is incomplete.

Best Answer

Label expressions are similar to field calculator: you don't need to use a for loop as the expression is already applied to every feature. Also, you'll get an error with your code as per the help:

Field values are automatically cast to text strings. Therefore, if you wish to use a numeric value in an arithmetic operation, or when making a comparison, you will need to cast it back to a numeric data type. The examples below add two integer fields

Here it is displaying odd values:

def FindLabel([Frame_Number]):
  return [Frame_Number] if int([Frame_Number]) % 2 else "whatever you want here"

Alternatively, you can use a SQL expression in Label Manager to accomplish the same:

MOD(Frame_Number, 2) = 1

Related Question