Python in Field Calculator

4492
8
Jump to solution
04-01-2015 04:10 AM
KONPETROV
Occasional Contributor III

Hi, I am using ArcGis 10.2., I have a problem with an expression i have created in Python for field calculator. I want to process a column  with distances in an attribute table. I want the value in the first row to be minus 10, and starting from that row to abstract each one with the next row.

My columns i want to be like that:

MO       State

2000     (2000-10)-1950=40

1950     1950-1940=10

1940     1940-1925=15

1925     1925-1954=-29

1954

Should i click on State column to do my calculations?

Can't i put my column in a list, and treat it as it is?

In codeblock I can process something only by creating a def?

How complicated can that be??

I tried UpdateCursor, row.GetValue but i didn't manage to do it. I have read a lot of examples but none of them were very usefull for that occasion.

Thanks for your time

0 Kudos
1 Solution

Accepted Solutions
KONPETROV
Occasional Contributor III

finally solved! thank you all for your help

import arcpy

f = "c:/W/S/Sort.shp"

field = ["MO", "Statem"]

with arcpy.da.UpdateCursor(f, field, "FID = 0") as cursor:

     for row in cursor:

        print "A"+str(row[0])+"A"

        row[0] = row[0] - 10

        cursor.updateRow(row)

        print "B"+str(row[0])+"B"

        MO = row[0]

with arcpy.da.UpdateCursor(f, field, "FID >= 1") as cursor:

     for row in cursor:

             Statem = (MO - row[0])

             MO = row[0]

             cursor.updateRow([MO, Statem])

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

​to many questions...show us what you want and what you have tried

KONPETROV
Occasional Contributor III

in a field which is like:

2000

1950

1940

1925

1954

i want to abstract 10 from 2000 and start the abstraction from 1990.

So as to get a column un that form after the abstraction

40 (1990- 1950)

40

15

-29

...etc

I have created this code in Python which has also a loop but it doesn't work well inside codeblock:

I thought if i could put my field (MOBIL) as a list in MO

MO = [MOBIL]
L
= len(MO) - 1
M
= L - 1
MO
[0] = MO[0] - 10
  
for i in range(M😞
  MO
[i] = MO[i] - MO[i+1]
  
if MO[i] < 0:
  
break
  
else:

I have also tried this one just to make the value at the first row (-10):

import arcpy

arcpy.env.workspace = "c:/W/Pa/SORT.shp"

# Create a cursor on a feature class

cur = arcpy.da.UpdateCursor('SORT.shp')

# Loop through the rows in the attribute table

while row <= 1:

    # The variable sqMiles will get the value from the column

    Mo = row.getValue('MO')

    # Calculate how many acres

    Moi = (MO - 10)

    # Assign the acres to a column named 'Area_Acres'

   row.setValue('Statem', Moi)

    # Apply the change

    cur.updateRow(row)

#Just to see it in the ArcPy window

print int(MO)

with no result!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You are mixing up the two different types of update cursors.  You are creating an arcpy.da update cursor but are trying to work with it like an older update cursor.  Also the variables "Mo" and "MO" aren't the same, Python is case sensitive.

Does the following work for a test:

cur = arcpy.da.UpdateCursor('SORT.shp', ['MO', 'Statem'])
for row in cur:
    MO = row[0]
    Moi = (MO - 10)
    cur.updateRow([MO, Moi])
KONPETROV
Occasional Contributor III

I tried that but it abstracts from the entire MO 10 and puts them in Statem, however i want only the first row to be minus 10, and start the abstract from there, so i tested that form of code

cur = arcpy.da.UpdateCursor('SORT_B', ['MO', 'Statem']) 

MO = row[0] 

Moi = (MO - 10)

for row in cur:

    Moi = (MO - MO[row+1]) 

    cur.updateRow([MO, Moi]) 

but i get an error

"File "<string>", line 4, in <module>

RuntimeError: A column was specified that does not exist"

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It would be most helpful if you could provide a sample, a specific sample with both columns next to each other and 10 or so rows.

It seems you are trying to index the cursor much like you do lists in Python.  Although lists and cursors are both iterable, it doesn't make them the same in terms of methods and handling.

I can't say I understand what you are trying to do, but I think I am starting to find the ballpark.

cur = arcpy.da.UpdateCursor('SORT_B', ['MO', 'Statem'])  
next(cur)
MO = row[0]  
Moi = (MO - 10)
cur.updateRow([MO, Moi])
for row in cur:
    Moi = (row[0] - MO)
    MO = row[0]    
    cur.updateRow([MO, Moi])
KONPETROV
Occasional Contributor III

Unfortunately that code seems having a strange effect although i put in the first two rows

import arcpy

arcpy.env.workspace = "c:/.........". It can run only by Python Window and the results are hardly to describe

I wil try to make it more clear. I have a column MO with distances and i want the second field "State" to start from minus 10 the abstraction

MO       State

2000     (2000-10)-1950=40

1950     1950-1940=10

1940     1940=1925=15

1925     1925-1954=-29 and here it must stop because 1954 has no other number to be abstracted with

1954   

That's all. And i am trying to create that script in field calculator choosing the MO column, unsuccesful until now.

0 Kudos
SepheFox
Frequent Contributor

Hi Kon, since you have a different calculation for the first row than all the others, I think you will have to do that one separately. Try a simple script to calculate the value of the state field by subtracting the value of the next feature from the value of the current feature, and then calculate the value of the first row manually, by selecting that row only.

KONPETROV
Occasional Contributor III

finally solved! thank you all for your help

import arcpy

f = "c:/W/S/Sort.shp"

field = ["MO", "Statem"]

with arcpy.da.UpdateCursor(f, field, "FID = 0") as cursor:

     for row in cursor:

        print "A"+str(row[0])+"A"

        row[0] = row[0] - 10

        cursor.updateRow(row)

        print "B"+str(row[0])+"B"

        MO = row[0]

with arcpy.da.UpdateCursor(f, field, "FID >= 1") as cursor:

     for row in cursor:

             Statem = (MO - row[0])

             MO = row[0]

             cursor.updateRow([MO, Statem])