Field Calculation arcpy

709
2
Jump to solution
07-09-2012 07:53 AM
DeeptiPuri
New Contributor
Hi

I am developing a script to calculate the values of two fields (C and D) based on value of  field A:

A     C    D
0.2  1    1
0.2  1    2
0.2  1    3
0.4  2    1
0.5  3    1
0.5  3    2

So the idea is

n = 1
k =0
while i < endrow
{
  if a==a[i+1]
     c=n
     d = k+1
if a < a[i+1]
     i=i+1
     k=0
     c=n+1
     d= k+1
}

Not sure - cursor can help me. But is there any way for such comparison and update the column?

Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Occasional Contributor III
It's possible to do this in Field Calculator separately for each field C and D.

Pre-Logic Script Code for field C:
aCur = 0 c = 1 def incrementC(aField):   global aCur, c   if aCur == 0:     aCur = aField     return c   elif aCur == aField:     return c   else:     c +=1     aCur = aField     return c

C =
incrementC(!A!)


Pre-Logic Script Code for field 😧
aCur = 0 d = 1 def incrementD(aField):   global aCur, d   if aCur == 0:     aCur = aField     return d   elif aCur == aField:     d += 1     return d   else:     d = 1     aCur = aField     return d

D =
incrementD(!A!)

Here's a result:
[ATTACH=CONFIG]15905[/ATTACH]

View solution in original post

0 Kudos
2 Replies
MarcinGasior
Occasional Contributor III
It's possible to do this in Field Calculator separately for each field C and D.

Pre-Logic Script Code for field C:
aCur = 0 c = 1 def incrementC(aField):   global aCur, c   if aCur == 0:     aCur = aField     return c   elif aCur == aField:     return c   else:     c +=1     aCur = aField     return c

C =
incrementC(!A!)


Pre-Logic Script Code for field 😧
aCur = 0 d = 1 def incrementD(aField):   global aCur, d   if aCur == 0:     aCur = aField     return d   elif aCur == aField:     d += 1     return d   else:     d = 1     aCur = aField     return d

D =
incrementD(!A!)

Here's a result:
[ATTACH=CONFIG]15905[/ATTACH]
0 Kudos
DeeptiPuri
New Contributor
Thanks Gastor! I want to use it as a GP model and got some hints yesterday- so resolved it. Your solution is slick too and work fine, Thanks!


It's possible to do this in Field Calculator separately for each field C and D.

Pre-Logic Script Code for field C:
aCur = 0
c = 1
def incrementC(aField):
  global aCur, c
  if aCur == 0:
    aCur = aField
    return c
  elif aCur == aField:
    return c
  else:
    c +=1
    aCur = aField
    return c

C =
incrementC(!A!)


Pre-Logic Script Code for field 😧
aCur = 0
d = 1
def incrementD(aField):
  global aCur, d
  if aCur == 0:
    aCur = aField
    return d
  elif aCur == aField:
    d += 1
    return d
  else:
    d = 1
    aCur = aField
    return d

D =
incrementD(!A!)

Here's a result:
[ATTACH=CONFIG]15905[/ATTACH]
0 Kudos