2

I have two fields (STREET1 & AKA_STREET_TEST1).

I'm having a problem creating a simple IF THEN Python statement in the Field Calculator box. I've been searching the threads for a similar example but to no avail. Basically the formula would work something like this.

If STREET1 contains "AVENUE", calculate AKA_STREET_TEST1 to "AVE", ELSE leave it blank

(example below)

database_sample Below is my feeble attempt at creating the if-then statement.

FieldCalculator_sample

  • finally what is right answer? I have exactly this problem.... – Ali Dec 2 '16 at 20:56
3

A couple things to note:

Your code block is currently taking two arguments, but you really only need to take one. AKA_STREET_TEST1 is the result you want, not something that Calc() will consider when calculating. So your expression should be Calc(!STREET1!) -- because you want the function Calc to do something using the information from field STREET1, and that value will then be assigned to AKA_STREET_TEST1.

You can use a simple, one-line operation with Python's string.replace function, which essentially incorporates your if-then logic automatically. If AVENUE is in the string, it will be replaced with AVE. If AVENUE isn't there, it returns nothing and should leave the attribute empty.

def Calc(value):
    if "AVENUE" in value:
        return value.replace('AVENUE','AVE')
2

It sounds like you've got the right idea but you're testing if STREET1 is perfectly equal to "AVENUE". Because STREET1 has the road number in it though, it will never be equal. Therefore, you would need to see if it contains AVENUE, not just is equal to AVENUE. If you use the python find() function on a string and it doesn't contain the value you're trying to find, it will return a value of -1, so try something like:

def Calc(STREET1):
    if STREET1.find("AVENUE") != -1:
        return STREET1.replace("AVENUE","AVE")


Calc(!STREET1!)
2

Try this if you are updating existing field and @radouxju method for populating new field:

def Calc(STREET1):
    return STREET1.replace('AVENUE','AVE')
2

you could use the find() function

def Calc(street):
    if street.upper().find("AVENUE") >=0:
        return street.upper().replace("AVENUE","AVE")

Calc(!street!)

Note that Python is case sensitive, so I added upper() to take all cases into account

  • Find method is not necessary. – artwork21 Jan 22 '15 at 19:54
  • 2
    it is, because the OP wants to leave it blank otherwise – radouxju Jan 22 '15 at 19:57
  • or you need to select first with [street] LIKE 'AVENUE%' – radouxju Jan 22 '15 at 19:57
  • 1
    Correct, if not populating to new field. – artwork21 Jan 22 '15 at 20:03
-2
def calc(a):
 if "AVENUE" in a:
  return "AVE"

calc (!STREET1!)
  • This will strip characters after AVENUE (i.e. "AVENUE 131" just becomes "AVE" instead of "AVE 131") – Erica Jan 22 '15 at 19:56
  • Agree. Didn't read post carefully – FelixIP Jan 22 '15 at 20:02
  • Its also been flagged for length and content. You'll usually need to provide at least some context around it (maybe an explanation, or caveats on use). – BradHards Jan 22 '15 at 21:13
  • Agree, my fault. Got distracted – FelixIP Jan 23 '15 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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