I have been trying to rework attributes in a polygon shapefile and think I have figured out how to ask in a sensible manor.

I need to fill column Field2 with a 1 IF the value in column Field1 is equal to a certain value.

For instance I want a 1 in my field "Greenspace" if my field "leisure" has the value 'park' or 'golf_course' in the column.


On field Greenspace open Field Calculator, set to Python Parser, and turn on Code Block.

In the Code Block:

def rework(leisure, greenspace):
    if leisure == 'park' or leisure == 'golf_course':
        return 1
        return greenspace

And in the expression:

rework(!leisure!, !Greenspace!)

The return greenspace here leaves any existing values in Greenspace. If this isn't here the other values in the column will be set to Null

enter image description here

You can also put it all on a single line (without using the Code Block) which is nice and tidy if this is the only possible combination, but if you have other values (which you want to return as 2 or 3 etc.) it will be easier to use the code block above.

1 if !leisure! == "golf_course" or !leisure! == "park" else !Greenspace!

enter image description here

In response to your comments, you can also try:

def rework(leisure, greenspace):
    if leisure in ['pitch', 'golf_course', 'sports_centre']:
        return 'sports' 
    elif leisure in ['grass', 'park', 'playground']:
        return 'park'
    elif leisure in ['garden', 'trees']
        return 'vegetation'
        return greenspace 
  • Should I be able to use '_' in my code? I have gotten the code to work twice but having Error 000989:... invalid syntax coming up on a lot of attempts. – Askren Ryan Feb 13 '17 at 3:07
  • @AskrenRyan you mean like in golf_course? – Midavalo Feb 13 '17 at 3:08
  • Good point. I have no idea where the error is coming from. – Askren Ryan Feb 13 '17 at 3:17
  • Character limit? Is there a way to nest several statements to fill im "greenspace" with different values based on different criteria? I should maybe start a new question... – Askren Ryan Feb 13 '17 at 3:21
  • @AskrenRyan I've added an example at the end which should allow you to specify different criteria to give different values, using python's if/elif/else – Midavalo Feb 13 '17 at 3:28

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.