1

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.

1

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
    else:
        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'
    else: 
        return greenspace 
1
  • Comments are not for extended discussion; this conversation has been moved to chat. – Midavalo Feb 13 '17 at 3:35

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.