1

this seems like it should be easy but I am new to python and am having trouble... I have four columns. I would like perform a field calculation that sums the values by row for three of the columns and inserts that sum into the associated row of the 4th column. In vb script is it very straight forward. I would like to know how to do it in python. ie.

Col1    Col2    Col3    Col4(SummedValues)
  1       3     1              5
  2       1     1              4

Here is what I have:enter image description here

I have tried adding a for statement (for rows in !resuly!:) but it returns a parsing syntax error (Error 000989 Python Syntax Error: Parsing error Syntax Error: invalid syntax (line 2)). enter image description here

I am in ArcMap V.10.4

I feel like it is an iteration issue- maybe I need to tell it to go through each row i just don't know how to do that correctly in the field calculator...


Per Phillip and JR comments: Code updated to and it worked perfectly.

enter image description here

3
  • 1
    Welcome to GIS.SE. What software are you using?
    – Erik
    Mar 25, 2019 at 14:57
  • Seems like you don´t need the x-parameter at all, as you´re returning the sum anyway. Mar 25, 2019 at 14:58
  • 1
    Please post the actual error message that goes with the code sample you show. The one you quote in your question is for a different version of your snipped. Mar 25, 2019 at 15:04

2 Answers 2

2

I see a few issues with the code you show:

  1. Your indentation is off - all lines after the def ... line should be on the same indentation level.
  2. You need to pass the fields you want to add to your function, not the field you want to populate. So your definition should be something like def addition(a, b, c):. Whatever you have in the second box is what gets populated into the field, in this case, the result of your function, its return value.
  3. For a simple thing like this, you should be able to do this without even using the codeblock. Just type !ones! + !tws! + !threes! into the second field, and you can remove whatever is in the codeblock field. As long as all the fields you are adding are of numeric type, this will work.
6
  • Thanks Phillip! so the return statement should also be indented? I can do it successfully with the simple field calculator. I am trying to learn Python however so wanted to know how to do something simple in the python code block.
    – sophiat4
    Mar 25, 2019 at 17:07
  • @sophiat4 if you want to learn python do yourself a favor and download the PyCharm IDE. It will show you when your syntax is incorrect.
    – atxgis
    Mar 25, 2019 at 17:15
  • I changed my indentation and input your suggestion but it still gives the same error. I tried to input a snippet into this text box to show you my new code but it wouldn't allow it. The variables, x, and the return statement are all indented 4 spaces from the def statement. my new code per your suggestions reads: def addition(a, b, c): a = !ones! b = !tws! c = !threes! x = a+b+c return x
    – sophiat4
    Mar 25, 2019 at 17:20
  • As said by PhilippNagel your def should be like "def addition(a, b, c):" then in the below box you replace the a, b and c by the actual field whose value you want to sum, so you get "addition (!one! , !tws! , !threes!)". Also the field calculator take care of iterating on each field you dont have to worry about that (in a standalone script that's different...)
    – J.R
    Mar 25, 2019 at 17:24
  • and you dont need to specify the field in the first box. so first box : def addition(a, b, c): // x=a+b+c // return x and in the below box addition (!one! , !tws! , !threes!)
    – J.R
    Mar 25, 2019 at 17:30
-1

enter image description here

Per @PhillipNagel and @J.R this code block worked.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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