Python Label Expression

4929
9
Jump to solution
04-30-2015 09:36 AM
ScottLamon1
Occasional Contributor

I am trying to create a python label expression without having any python skills

I have 2 fields [FIELD1] and [FIELD2]

If [FIELD1] > 0 and [FIELD2] < 0

return "text:" [FIELD1] 'text:" [FIELD2]

If [FIELD1] > 0 and [FIELD2] !< 0

return "text:" [FIELD1]

If [FIELD1] !> 0 and [FIELD2] < 0

return 'text:" [FIELD2]

Any help would be greatly appreciated.

Tags (2)
1 Solution

Accepted Solutions
SepheFox
Frequent Contributor

You could also define label classes based on those if statements, and then label each class accordingly.

View solution in original post

9 Replies
DarrenWiens2
MVP Honored Contributor

Please refer to this help page as a start for building label expressions, and this post for posting code blocks.

DanPatterson_Retired
MVP Emeritus
Header 1Header 2

If [FIELD1] > 0 and [FIELD2] < 0

return "text:" [FIELD1] 'text:" [FIELD2]

if (!FIELD1!  > 0) and (!FIELD2!  <  0):

    out = str(!FIELD1!) + str(!FIELD2!)

If [FIELD1] > 0 and [FIELD2] !< 0

return "text:" [FIELD1]

elif  (!FIELD1!  > 0) and (!FIELD2!  !=  0):

    out = str(!FIELD1!)

If [FIELD1] !> 0 and [FIELD2] < 0

return 'text:" [FIELD2]

elif (!FIELD1!  > 0) and (!FIELD2!  !=  0):

    out = str(!FIELD1!)

else

    out = "no match'

return out

Lets break this down into parts

Toggle on the Python parser, in the field calculator.  The results must be put into a text field. 

So as python code it becomes with 4 spaces for indents.

def check_cond(a,b):

    """two fields as input, """

    if a  > 0) and b  <  0):

        out = str(!FIELD1!) + str(!FIELD2!)

    elif  a  > 0) and b  !=  0):

        out = str(!FIELD1!)

    elif a  > 0) and b  !=  0):

        out = str(a)

    else

        out = "no match'

         return out

Expression box

check_cond(!FIELD1!,!FIELD2!)

Always check the unexpected, never return prematurely, hence return out should be the last line

DarrenWiens2
MVP Honored Contributor

Just a note that this is a field calculator expression, not label expression, which is quite different. Of course, you could calculate the label to a new field, but that's an added step.

DarrenWiens2
MVP Honored Contributor

There are a few tricky little things, so here goes. I don't believe there is such thing as !<, use >= instead. You can't concatenate numbers to strings, cast to string using str(). You'll have to decide if you really mean "if, if, if" or "if, elif, elif" - either can be used if you know what it means.

def FindLabel ( [Field1] , [Field2]  ):
    if [Field1] > 0 and [Field2] < 0:
        return "text: " + str( [Field1] )+ " text: " + str( [Field2] )
    if [Field1] > 0 and [Field2] >= 0:
        text = "text: " + str([Field1] )
        return text
    if [Field1] <= 0 and [Field2] < 0:
        return "text: " + str( [Field2] )
SepheFox
Frequent Contributor

You could also define label classes based on those if statements, and then label each class accordingly.

ScottLamon1
Occasional Contributor

I went with the various label classes.  Thanks for all the info though.

0 Kudos
SepheFox
Frequent Contributor

I'm glad you found a solution that worked for you, Scott. Do you mind marking this questions as answered? This accomplishes a couple of things: people will realize that you don't still need help, and also, other people in the future with the same question can find the answer easier. Thanks!

0 Kudos
curtvprice
MVP Esteemed Contributor

I'm a moderator and marked your suggestion correct.

SepheFox
Frequent Contributor

Thanks Curtis!

0 Kudos