I have problem with Field Calculator, using ArcGIS 10.2. I tried to write a statement but I don't know what is wrong.
I have two columns:
The first contains a number code. Every code represents some text data. For example: "1" means "trees,etc."
The second column (string) is empty and should contain text data. Could you help me with Python ?
-
3Publish the Python code you are running, we will take a look.Alex Tereshenkov– Alex Tereshenkov2015年08月25日 14:56:25 +00:00Commented Aug 25, 2015 at 14:56
-
Below i pasted codesayeret.poland– sayeret.poland2015年08月25日 15:43:49 +00:00Commented Aug 25, 2015 at 15:43
1 Answer 1
Your code is not correctly formatted Python code:
- Indentation is important in Python, as it defines where functions and conditions start and end.
- You need a
:
after the function definition - The correct operator for equality-comparison is the
==
and not simply=
which assigns a value to a variable.
Also, with respect to the field calculator in ArcGIS, if you use a function to calculate your value, you also will need to check the "Show Codeblock" check box.
In your case, you have two possibilities:
Your if/else-statement is very simple, so you could rewrite in a single Python line, and there is no need for a codeblock:
"SampleText" if !Coded! == 1 else "SampleText_2"
Using a code block, with the code block checkbox activated, the code block should be:
def Text_d(Coded): if Coded == 1: return "SampleText" else: return "SampleText_2"
and then in the "Coded=" box below you would need to write:
Text_d(!Coded!)
This second method gives you the possibility to use many Python functionalities such as nested conditions:
def Text_d(Coded): if Coded == 1: return "SampleText" elif Coded == 2: return "SampleText_2" else: return "SampleText_3"
-
Ok, that way helped me but how to calculate for more conditions? I mean more "if`s", f.e 1 =="sample text_1", 2 == "sample_text_2", etc.sayeret.poland– sayeret.poland2015年08月25日 16:54:53 +00:00Commented Aug 25, 2015 at 16:54
-
I tried use it for more conditions and didn
t work :/ Any idea what
s wrong?sayeret.poland– sayeret.poland2015年08月25日 17:09:09 +00:00Commented Aug 25, 2015 at 17:09 -
It was mistake in last line of code (else Coded == 3, should be else:)sayeret.poland– sayeret.poland2015年08月25日 17:23:05 +00:00Commented Aug 25, 2015 at 17:23
Explore related questions
See similar questions with these tags.