Populate new field ID based on conditions from other fields

430
2
Jump to solution
09-05-2019 01:51 PM
nonipaulette
New Contributor II

Hi everyone.

I am trying to populate a field using unique identification based on two existing fields.

Background: I ran the Density-Based Clustering tool on a set of points. The output creates a new field called 'Cluster ID' which groups clusters sequentially. If the input points are not a cluster they are assigned "-1". I created a new field called 'UOID' which copies the field from the 'Cluster ID' unless "-1", in that case it copies the field from the 'Source ID'.

Problem: There are instances where both fields will be copied to the new field 'UOID' (for instance the number 1). Is there a python script that will allow me to populate the new field 'UOID' adding unique identification to distinguish which fields were copied? For instance can i put a "C" in front of the copied fields from the 'Cluster ID' field and a "S" in front of the 'Source ID'? Take a look at my data. Long story short i need a way to distinguish which fields were copied using unique identification. 

Thank you very much!!

0 Kudos
1 Solution

Accepted Solutions
nonipaulette
New Contributor II

Worked perfectly Lance. Now I can copy the appropriate fields, while ensuring they are uniquely identifiable. THanks alot!!

View solution in original post

0 Kudos
2 Replies
LanceCole
MVP Regular Contributor

Noni

Using field calculator create a code block such as the following:

def uio(cluster, source):
  if cluster > 0:
    return 'C%s' %cluster
  else:
    return 'S%s' %source‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The %s is replaced with the cluster or source value C1, S10, etc.  Make sure the field you are placing the return into is Text and not Numeric.  You would call the function using the field names:

uio(!cluster!, !source!)

nonipaulette
New Contributor II

Worked perfectly Lance. Now I can copy the appropriate fields, while ensuring they are uniquely identifiable. THanks alot!!

0 Kudos