Update all the odd rows in a field with the value in the row above.

721
5
Jump to solution
01-10-2014 07:13 AM
ClarkMitten
New Contributor
I need to update a field  where all of the even rows have a value and the odd row below needs that value assigned to it as well. 
This needs to become  This
1        -                       1
0        -                       1
2        -                       2
0        -                       2
1        -                       1
0         -                      1

I ideally would like to run the code as a calculate field within model builder.

For more information every two rows has the same ID. For example the first 2 rows have ID A the next two, B.  I don't know if this would help solving those second values or not.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
This worked for me:

import arcpy  def UpdateTable(table, field):      # Start update cursor on table     ind = []     start = 1     rows = arcpy.UpdateCursor(table)     for row in rows:         ind.append(row.getValue(field))         row.setValue(field, ind[0])         start += 1         if start % 2 != 0:             ind = []         rows.updateRow(row)     del row, rows     return  test = r'C:\TEMP\New_Points_test\another_Centroid.shp' UpdateTable(test, 'Test') print 'done'


You could save this as a script tool and then embed it into your model.  the table parameter is the table you want updated, the field is the field with the values.  It will update that same field.

View solution in original post

5 Replies
by Anonymous User
Not applicable
You can select all odd rows with this query:

Round(Round( "OBJECTID" , 0) / 2, 0) <> Round( "OBJECTID" , 0) / 2


Or even by switching your selection or this this query:

Round(Round( "OBJECTID" , 0) / 2, 0) = Round( "OBJECTID" , 0) / 2


You can then do your field calculators with each selection.  You can see some more info on this [url=http://forums.arcgis.com/threads/86961-I-WANT-TO-CONSTRUCT-AN-SQL-STATEMENT-TO-SELECT-EVEN-OR-ODD-FI...]
ClarkMitten
New Contributor
I can see how selecting all the odd rows would be a start.  I don't know of a field calculator query that can pull in data from the same field though?

I should probably say that it isn't a big deal if I need to run this in a script rather than trying to work it into model builder.
0 Kudos
by Anonymous User
Not applicable
What you want to do can be accomplished using an update" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help../index.htm... cursor.  I do not really understand what you are trying to do though.  In your first post you have:


1 - 1
0 - 1
2 - 2
0 - 2
1 - 1
0 - 1


What is the column on the left?  Can you explain this a little more?  If you just need all rows in pairs of twos to have the same alternating value of 1 or 2, that would be simple with a cursor.
ClarkMitten
New Contributor
What you want to do can be accomplished using an update cursor.  I do not really understand what you are trying to do though.  In your first post you have:



What is the column on the left?  Can you explain this a little more?  If you just need all rows in pairs of twos to have the same alternating value of 1 or 2, that would be simple with a cursor.


Sorry for the confusion.  The column on the left is what I have currently. On the right is what I'd like to generate.  I was just creating a simple example.  It won't just be 1's and 2's it will be volume data so it could be 32.1 on row 1 and 64 on line three.  In that case i need to find a way to get row two to also say 32.1 and row 4 to say 64.
0 Kudos
by Anonymous User
Not applicable
This worked for me:

import arcpy  def UpdateTable(table, field):      # Start update cursor on table     ind = []     start = 1     rows = arcpy.UpdateCursor(table)     for row in rows:         ind.append(row.getValue(field))         row.setValue(field, ind[0])         start += 1         if start % 2 != 0:             ind = []         rows.updateRow(row)     del row, rows     return  test = r'C:\TEMP\New_Points_test\another_Centroid.shp' UpdateTable(test, 'Test') print 'done'


You could save this as a script tool and then embed it into your model.  the table parameter is the table you want updated, the field is the field with the values.  It will update that same field.