Tuesday, 7 February 2017

python - Field Calculator If/Then in ArcGis 10.1


I've been looking through these threads, i found something similar and thought it could be the answer, but it doen't seem to work for me...


I could do this the slow way with Select By Attributes, but It's time consuming and I will have to repeat it several times for other works.


I have a field called "Kultur" (String) with different codes (DG, A, FF, ...). I want to change this information into a numeric information, so i added a new field called "Kultur_Cl" (Short Integer).


In my case if "Kultur" is DG, "Kultur_Cl" should be 1. If "Kultur" is A, "Kultur_Cl" should be 2, ...


Here is what i tried :


enter image description here



And here is what it looks like in ArcMap:


enter image description here


Once it's done i would like to do : If "Kultur_Cl" = 1 then "Kultur_Pkt" = 15, if "Kultur_Cl" = 2 then "Kultur_Pkt" = 18, ... Would this go the same way, or is it different because there would only be numeric data?


Thanks for your help !!



Answer



first you need a double == for your test, simple = is for assignment, not testing


second you forgot the indentation and a semi-colon


def MyCalc(Kultur,DG):
if (Kultur == DG):
return 1

else:
return 2

third, DG must be identified as a string


MyCalc(!Kultur!,'DG')

it would go the same way with numeric values (you don't need quotes in this case)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...