# Field calculation- Arcpy

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

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[i]==a[i+1]
c[i]=n
d[i] = k+1
if a[i] < a[i+1]
i=i+1
k=0
c[i]=n+1
d[i]= k+1
}


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

Thanks

adding another complication- field E which has the maximum D for every unique A or C; so it will be:

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


Because my ultimately goal is to keep only D ==1 and label the remained points with C and E (say: there are 3 points at location 1)

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


Right now appears, I have to run another combination of search and update cursor after calculation of C and D but if there is any other concise and sleek approach! If I will figure it out - I will post it here!

• The methods described at gis.stackexchange.com/questions/16752 and gis.stackexchange.com/questions/27047 may solve this problem for you. (I'm pretty sure exactly this question has been asked and answered on this site, but I have been unable to find the duplicate.) Jul 9, 2012 at 18:35
• Finished the task but instead of keeping the max value for D, assign a boolean variable.
– Deep
Jul 12, 2012 at 13:19

Without indents, I'm not sure what you mean your code to do. From the table you gave, it looks like you want C to count the number of unique values up to that point, and D to count the number of times the current value of A has recurred (so once is 1, twice in a row is 2, etc.) If this is right, and if the values in A are ascending, this should work (I'm at home so I can't check with arcpy, unfortunately):

import arcpy

# create the cursor and populate the C and D fields of the
# first row.
cur = arcpy.UpdateCursor('TableNameHere')
row = cur.next()
last = row.getValue('A') # the last value in field A

row.setValue('C', 1)
row.setValue('D', 1)
cur.updateRow(row)

run = 1 # how many values in this run
count = 1 # how many runs so far, including the current one

# the for loop should begin from row 2, since
# cur.next() has already been called once.
for row in cur:
current = row.getValue('A')

if current == last:
run += 1
else:
run = 1
count += 1
row.setValue('C', count)
row.setValue('D', run)
cur.updateRow(row)

last = current

# release the layer from locks
del row, cur


Substitute the path of your table for TableNameHere and the correct field names for A, B and C, of course.

This is a rough cut at your problem; I haven't tested it out, but the logic should generally be what you need:

import arcpy

tbl = r'path\to\db.mdb\table'

#-Get field objects in table
fields = arcpy.ListFields(tbl)

#-Create an empty list to hold row data in memory
data = []

rows = arcpy.SearchCursor(tbl)
for row in rows:
#-Create an empty dictionary to store each row
d = {}
for f in fields:
d[f.name] = row.getValue(f.name)
data.append(d)
del row,rows

n = 1
k = 0
i = 0
less_than = False
rows = arcpy.UpdateCursor(tbl)
for row in rows:
if less_than:
#-update rows c and d with the appropriate values
row.c = n+1
row.d = k+1
less_than = False
else:
if row.a == data[i+1][a]:
#-update rows c and d with the appropriate values
row.c = n
row.d = k+1
elif row.a < data[i+1][a]:
#-set flag and skip to the next row
less_than = True
i+=1
rows.updateRow(row)
del row,rows