Python-Creating Short Label Field Based on Another Field

721
4
05-18-2017 04:31 AM
DonalCasey1
New Contributor

I have a field in the attribute table of ArcMap that contains either the value 'Freehold' or 'Leasehold'. I have another field that contains a unique ID of each polygon. I would like to create a third field called 'Short Label' that I can use to label everything from F1..F60  and L1...L60 based on the Tenure in my table. I would then use this as my label field and I would insert a table into the layout as a lookup.

Would anybody be able to help me write a python script to create the short label field based on the Tenure type?

Thanks

TenureUnique IDShort Label
FreeholdEGL2266995F1
LeaseholdNGL4459756L1
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

Is that an actual portion of the table or a replica.

What is the association between the Freeholder/leaseholder and the unique ID field.

If you have a Tenure column as shown, getting the F or L is easy.. slice it  

python parser, !Tenure![0] will get the first letter... it is the numbers to concatenate with then is the key.

DonalCasey1
New Contributor

Dan_Patterson That is just a replica of the table, I put in examples of what I need the short label to be.

There is no association between the freehold/leaseholder and the unique ID field. I simple want the person reading the map in the end to know that if a label has the prefix 'F' that it is a freehold and 'L' is a leasehold. If I slice the tenure column like demonstrated above is it possible to then suffix with numbers? Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

There is always a way... this is via a demonstration.  If it is what you want, it can be simplified and used in a code block in the field calculator.  Let me know if there are any other variants.  I used 'F' and 'N' to represent your cases because I felt like it....

a = ['F', 'F', 'N', 'N', 'F', 'N', 'F', 'N', 'F', 'F']
f0 = 0
n0 = 0
def hdr(a):
    global f0
    global n0
    frst = a[0]
    if frst == 'F':
        rtrn = "F{}".format(f0)
        f0 += 1
    else:
        rtrn = "N{}".format(n0)
        n0 += 1
    return rtrn

for i in a:
    print(hdr(i))

Which returns

# commentary added after the print run
F0  # F found first
F1  # then again
N0  # N found first
N1  # then again
F2  # back to F
N2  # ooopsie another N
F3  # and so on
N3
F4
F5
JoshuaBixby
MVP Esteemed Contributor

Using Dan's example as an example, Python's itertools module has an increment counter if you don't want to use global.

from itertools import count

a = ['F', 'F', 'N', 'N', 'F', 'N', 'F', 'N', 'F', 'F']
fcount = count()
ncount = count()
def hdr(a):
    frst = a[0]
    if frst == 'F':
        rtrn = "F{}".format(fcount.next())
    else:
        rtrn = "N{}".format(ncount.next())
    return rtrn