Sequentially numbering but only for certain rows in an attribute table

1355
10
Jump to solution
05-20-2022 11:56 AM
Labels (1)
AlexZacher
New Contributor II

Hello!

I want to sequentially number only particular features (headstones) in a table with location data for different types of stones, and I'm having trouble figuring out how to code this in Python.

I keep getting errors when I use SequentialNumber() and try to call the headstone field in the code block. Typing !Headstone! instead gives a syntax error.

Screenshot (519).png

Basically, I want to be able to number (and later label) only the headstones. Their order in the table is important too.

 Is there a better way to do this? Any help would be greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor

 

rec = Headstone * (rec + pInterval)

 

When the "Headstone" value = 0 (not a headstone), then "rec" becomes zero as you multiplied it by zero.

 

    if (rec == 0):
        rec = pStart

 

So, the next row, since it now see's "rec=0" will reset to pStart.  That is why it is "resetting" when not a headstone.

You can simply put a definition query on the layer where Headstone = 1.

then just use the SequentialNumber() as is and it will only calculate the sequential numbers for the Headstones (rows included in the definition query).

RhettZufelt_0-1653078681301.png

R_

View solution in original post

10 Replies
AndyAnderson
Occasional Contributor II

Did you try the assignment

Number = 

SequentialNumber(!Headstone!)

? — Andy

0 Kudos
AlexZacher
New Contributor II

Hi Andy,

Thanks for your response! I have tried that. I get a different error with that method, stating:

ERROR 000539: Traceback (most recent call last):
  File "<expression>", line 1, in <module>
TypeError: SequentialNumber() takes 0 positional arguments but 1 was given
Failed to execute (CalculateField).   

 

0 Kudos
AndyAnderson
Occasional Contributor II

And was the function still written as in the picture above?

def SequentialNumber(Headstone)

This variable Headstone is different than the variable  !Headstone! used to call the function.

— Andy

AlexZacher
New Contributor II

I see! It was not when I got that error.

With the changes you suggested, the calculation seems to work, however, it is only applied to the first row of the table -- see the highlighted cell below.

Screenshot (523).png

I did initially try to sort the values in the Headstone field by descending order, thinking that I might sequentially number the rows after sorting them, then ask ArcGIS to stop labeling after a certain value. But the sorting method I tried seemed temporary, and I had some issues with field calculations getting stuck in the first row after that.

0 Kudos
AndyAnderson
Occasional Contributor II

The calculation is only applied to the first row because you have it selected. Click the deselect button at the top and try again. (This is a “feature” though one that I often forget to avoid!)

— Andy

AlexZacher
New Contributor II

Thank you! Wow, that had really been bothering me because I couldn't figure out what "fixed" the first row calculations earlier.

That is one problem solved, however, now it seems that the "return rec" line at the end of the code block causes the values in the new field to reset to zero after encountering a cell that is not a headstone (value = 0)... or something like that.

Screenshot (524).png

Is this a situation where learning how to program a loop might help?

0 Kudos
AndyAnderson
Occasional Contributor II

Re «"return rec" line at the end of the code block causes the values in the new field to reset to zero after encountering a cell that is not a headstone (value = 0)... or something like that.»

It’s not the statement

return rec

that’s resetting rec, it’s the statement

rec = Headstone * (rec + pinterval)

that’s doing it. You’re resetting rec to zero whenever Headstone is zero. So just use this statement:

rec += pinterval

but also add this statement as the first line in the function:

if Headstone == 0: return 0

— Andy

0 Kudos
AlexZacher
New Contributor II

You know what. That actually ended up being the solution to my problem!

I turned off all layers except the headstones, selected all of them, then ran the calculation from above. It only numbered the selected features sequentially.

Thank you so much for your help in this matter!

0 Kudos
RhettZufelt
MVP Frequent Contributor

 

rec = Headstone * (rec + pInterval)

 

When the "Headstone" value = 0 (not a headstone), then "rec" becomes zero as you multiplied it by zero.

 

    if (rec == 0):
        rec = pStart

 

So, the next row, since it now see's "rec=0" will reset to pStart.  That is why it is "resetting" when not a headstone.

You can simply put a definition query on the layer where Headstone = 1.

then just use the SequentialNumber() as is and it will only calculate the sequential numbers for the Headstones (rows included in the definition query).

RhettZufelt_0-1653078681301.png

R_