2

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 Aug 6 '13 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 Aug 17 '18 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 Review – Dan C Aug 17 '18 at 16:13
3

I'm not sure how green you are, so here are "pretty explicit" instructions...

  1. Open up Field Calculator on the Point_ID field
  2. At the top, choose the "Python" parser
  3. Click the checkbox beside "Show Codeblock"
  4. 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
    
  5. 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 Aug 6 '13 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.