I need to calculate a new field with a python conditional where some values only occur if a sequence in a separate attribute field (X) occurs.
For instance my data set looks like this. Y is the attribute I want to fill out:
ID X Y
1 1 -
2 1 -
3 0 -
4 0 -
5 0 -
6 1 -
7 1 -
8 1 -
9 0 -
10 0 -
11 1 -
Values for Y:
If X = 1, Y = 1
If X = 0 and previous X = 1, Y = 2
If X = 0 and previous x = 0, Y = 0
So my data would look like:
ID X Y
1 1 1
2 1 1
3 0 2
4 0 0
5 0 0
6 1 1
7 1 1
8 1 1
9 0 2
10 0 0
11 1 1
I have seen previous lines stored using:
prev_X = ""
But don't know how to incorporate it into my script which looks like:
fc = r'.....'
rows = arcpy.UpdateCursor(fc)
Y = 1
prev_X = "" #<=========== DOESN'T WORK
for row in rows:
if row.X == 1:
Y = 1
row.Y = Y
rows.updateRow(row)
while row.X == 0 and prev_X == 1:
Y = 2
row.Y = Y
rows.updateRow(row)
while row.X == 0 ande prev_X == 0:
Y = 0
row.Y = Y
rows.updateRow(row)
rows.next()
row.X = X
rows.updateRow(row)
del row, rows