Field Calculator Python - Comparing one field to the next

2274
5
Jump to solution
04-02-2012 10:06 AM
MikeMacRae
Occasional Contributor III
I have the following syntax in a coded block script in field calculator using python and a personal geodatabase feature class:

if !ECOSITE! == !ECOSITE_1!:     test = "Same" else:     test = "Inclusion"  COMPARE_FIELDS =  test


Maybe it's a Monday morning and I just can't seem to figure out why it's erroring out, but it is. I am getting one or 2 error numbers....the generic 999999 and another generic one 000989. Neither one of which are revealing why the calculator doesn't like my equation. Can anyone identify what it going wrong? I've also attached a screenshot of my field calculator.

Thanks,
Mike

[ATTACH=CONFIG]13186[/ATTACH]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor
Mike, you're getting close. It's a funny syntax thing - in the expression you reference fields (e.g. !ECOSITE!), then they turn into arguments/variables in the function (e.g. ECOSITE). Then, "return" the values.

def TestFields ( ECOSITE , ECOSITE_1 ): # these reference individual values     if ECOSITE_1 == ECOSITE:         return "Same"     else:         return "Inclusion"  COMPARE_FIELDS =  TestFields(!ECOSITE!,!ECOSITE_1!) # these reference the field to grab individual values

View solution in original post

0 Kudos
5 Replies
CoryMacNeil1
Occasional Contributor II
Hi Mike,

Python is a bit different for its setup.  You need to have a function definition and a call to the function.  Try this:
def TestFields (Field1,Field2):
    if Field1 == Field2
        test = "Same"
    else:
        test = "Inclusion"

COMPARE_FIELDS = 
TestFields(!ECOSITE!,!ECOSITE_1!)


I hope this helps.
Cheers,
Cory
0 Kudos
MikeMacRae
Occasional Contributor III
thanks for the response Cory. Unfortunately this still is returning the same errors.

I tried:

def TestFields ( !ECOSITE! , !ECOSITE_1! ):
    if !ECOSITE_1! == !ECOSITE!:
        test = "Same"
    else:
        test = "Inclusion"

COMPARE_FIELDS = 
TestFields(!ECOSITE!,!ECOSITE_1!)



I even tried returning the "test" variable and same deal...I'm stumped.

Also, I don't think you need to define a function in the field calculator in code block mode. I have other conditional scripts that work without defining them as functions.

Thanks,
Mike
0 Kudos
DarrenWiens2
MVP Honored Contributor
Mike, you're getting close. It's a funny syntax thing - in the expression you reference fields (e.g. !ECOSITE!), then they turn into arguments/variables in the function (e.g. ECOSITE). Then, "return" the values.

def TestFields ( ECOSITE , ECOSITE_1 ): # these reference individual values     if ECOSITE_1 == ECOSITE:         return "Same"     else:         return "Inclusion"  COMPARE_FIELDS =  TestFields(!ECOSITE!,!ECOSITE_1!) # these reference the field to grab individual values
0 Kudos
CoryMacNeil1
Occasional Contributor II
Hi Mike,

With Python, I am quite certain you do have to define a function.

Try this (I forgot the return in my first posting):
def TestFields (Field1,Field2):
    if Field1 == Field2:
       return "Same"
    else:
        return "Inclusion"

TEST =
TestFields( !ECOSITE!, !ECOSITE_1! )

I just did a sample and it works.  If it does not work for you, let me know what data types your two ECOSITE fields are.

I hope this helps.
Cory
0 Kudos
MikeMacRae
Occasional Contributor III
Hey thanks folks!

Darren that works. I started playing around with returning the values. Thanks for pointing me in the right direction

Cory, looks like you followed up the same thought. Also, you are right about defining functions. I was looking at an older script of mine, but it turns out it was in VB. My bad!

Mike
0 Kudos