Select to view content in your preferred language

# How to create sequential numbers based on the count of values from another field?

624
2
05-01-2023 11:32 PM
Labels (2)
New Contributor

Hello everyone,

I need some help with a python code to populate a field with sequential numbers based on the count of another field in attribute table with field calculator. Like in the image, 10 x hm correspond to 1 km, but when count of hm <10 i need to summarize the last km with the values from hm field.

Any suggestions?

ArcMap 10.8.1

1 Solution

Accepted Solutions
MVP Frequent Contributor

km =

``get_km(!hm!)``

Code Block

``````count = len([row for row in arcpy.da.SearchCursor("CApa_hm_km", ["*"])])
last_full_km = int(count / 10)
current_km = 0

def get_km(hm):
global last_full_km
global current_km
current_km += hm / 1000
km = current_km + 1
if current_km < last_full_km:
km = int(km)
return km``````

Have a great day!
Johannes
2 Replies
MVP Frequent Contributor

km =

``get_km(!hm!)``

Code Block

``````count = len([row for row in arcpy.da.SearchCursor("CApa_hm_km", ["*"])])
last_full_km = int(count / 10)
current_km = 0

def get_km(hm):
global last_full_km
global current_km
current_km += hm / 1000
km = current_km + 1
if current_km < last_full_km:
km = int(km)
return km``````

Have a great day!
Johannes
New Contributor

Thank you very much! it works!