I am trying to figure out a solution to an field calculation problem. What I'm trying to do is automatically (using either ArcPy or the Field Calculator) generate sequential numbers, beginning at 1, in a new blank integer field (called 'Point_ID') for every recurrence of a value in a second field (called 'Line_ID'). The sorting of the sequential values in the second field ('Line_ID') will be based on the order in a third field ('FID'). Can anyone help me do this? I am very green when it comes to advanced field calculations and ArcPy (and Python in general, for that matter). So your explicitness is much appreciated.
Another way to put it: there are duplicate values in the Line_ID field. I would like to create a new field that counts up for every duplicate occurrence in the Line_ID field, with the sort order based on the FID field. So, if there are nine values in the Line_ID field that have the value "A," the new Point_ID field will go from 1 to 9, with the order based on the sort on FID.
-
I think ArcPy and the Field Calculator are both overkill for this. I would try using the Summary Statistics tool with a case_field of Line_ID first and if that does not give you a solution then perhaps add a picture of the input and expected output for your process to your question using the edit button. I'll +1 your question in case you have insufficient reputation to do that.PolyGeo– PolyGeo ♦2013年08月06日 05:54:07 +00:00Commented Aug 6, 2013 at 5:54
-
I had this same problem and had trouble working out the right python snippet to sue in the field calculator. Jason Miller's response works a treat!user126889– user1268892018年08月17日 15:48:33 +00:00Commented Aug 17, 2018 at 15:48
-
Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From ReviewDan C– Dan C2018年08月17日 16:13:31 +00:00Commented Aug 17, 2018 at 16:13
1 Answer 1
I'm not sure how green you are, so here are "pretty explicit" instructions...
- Open up Field Calculator on the Point_ID field
- At the top, choose the "Python" parser
- Click the checkbox beside "Show Codeblock"
In the "Pre-Logic script code", paste the following code...
prevFieldValue = '' counter = 1 def GetDuplicateCounter(myFieldValue): global prevFieldValue global counter if myFieldValue == prevFieldValue: counter += 1 else: counter = 1 prevFieldValue = myFieldValue return counter
In the "Point_ID = " box, type in
GetDuplicateCounter(!Line_ID!)
Note: If your Line_ID field is not a string field, then change the first line of code to prevFieldValue = 0
or something similar...
-
This worked like a charm. I'm only green when it comes to the syntax of the code itself; I understand the logic and how to use the field calculator. But thanks a lot for a perfect solution and for being extra explicit! Exactly what I was looking for.jchapman311– jchapman3112013年08月06日 17:34:55 +00:00Commented Aug 6, 2013 at 17:34
-
The order of my Line_ID was random (compared to FID). Just an extra step of sorting the table and it worked great.Ray J– Ray J2023年06月05日 18:58:47 +00:00Commented Jun 5, 2023 at 18:58
Explore related questions
See similar questions with these tags.