Select to view content in your preferred language

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

906
2
Jump to solution
05-01-2023 11:32 PM
Labels (2)
EmiliaBoc
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?

Thank you in advance!

ArcMap 10.8.1

hm_km.png

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Alum

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

View solution in original post

2 Replies
JohannesLindner
MVP Alum

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
EmiliaBoc
New Contributor

Thank you very much! it works!

0 Kudos