I need to increment a row in attribute table:

def func (w,v,pot):
 return pot + ((pot+1 - pot)/(v+1 - v))*(w-v)

The "+1" is not the addition of number one to the variable, is the intention to increment + 1 row in that column of that variable. This is because I need to calculate the actual record using records from the next line.

How can I do that? I know this is basic, but I realy don't know python.

  • Going a little further, is it possible to search the v records immediately lower and higher than w? And use just this values to do the interpolation? – AndM Oct 5 '15 at 12:34
  • Field calculator is great for simple functions, but once you want to start implementing look-ahead/behind, it becomes a lot simpler to just use cursors. They have a bit of a learning curve especially if you aren't familiar with Python. – Paul Oct 5 '15 at 17:43
  • Have a look at my solution in duplicate hyperlink, with 3 rows table – FelixIP Oct 5 '15 at 23:00

I am not aware of a technique for accessing the next record. But you can access the previous record using the field calculator. Using Python and a codeblock, you need to have a global variable that keeps the value of the previous record. That would be something like:

pot_previous = None
v_previous = None
def func (w,v,pot):
    global pot_previous
    global v_previous
    if pot_previous is None or v_previous is None:
        pot_previous = pot
        v_previous = v
    result_value = pot + ((pot_previous - pot) / (v_previous - v))*(w-v)
    pot_previous = pot
    v_previous = v
    return result_value

You can probably do what you was intending to do by sorting your records in reverse order of what you have intended initially.

Have also a look at this GIS.SE question and the ArcGIS resource center help (section "Accumulative and sequential calculations").

| improve this answer | |
  • 1
    Thank you so much for your help. Why did you put "global" before variables? And why in the end you need to write again "pot_previous = pot" and "v_previous = v? I'm sorry to ask that, but it's not intuitive to me. – AndM Oct 5 '15 at 12:26
  • 1
    Oh, that is to have access to the variables pot_previous and v_previous defined before the function from within the function. If you don't do this, a variable with name pot_previous inside your function is not the same as the one outside of your function, and without it you could not "remember" the values from the previous record. ArcGIS will make one run through the function for each record, but not initialize again the variables defined before the function. – chkaiser Oct 5 '15 at 16:41
  • 1
    For the pot_previous = pot and v_previous = v statements that are twice: the first one is just for the first record to remember the value but without doing interpolation (you need to skip the first record because you need the value of another row). The second time it is for updating the value so that in the next run you can access the value of the previous record. – chkaiser Oct 5 '15 at 16:42
  • 1
    Thanks for the explanation! I changed to: result_value = pot_previous + ((pot - pot_previous ) / (v - v_previous ))*(w-v_previous) because in interpolation I need to add the previous value, not the actual. But it's not giving me the expected result... – AndM Oct 5 '15 at 16:54

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