Copy specific field values using python in ArcGIS Pro

601
4
Jump to solution
08-08-2019 11:08 AM
nonipaulette
New Contributor II

Hi there everyone.

First of all thanks for helping, and I realize I need some tutorials in python and can hopefully address that soon.

Problem: Using the Calculate Field tool I would like copy values from two other fields based on specific criteria. For instance I have a new field called "UnathorizedOccupantID". I would like to copy values from the field called "ClusterID", however when there is a value of -1 I would like to copy the corresponding values from the "SourceID" field. As seen here:

I believe this will get me to the next part of the analysis which is aggregating the cluster ID's using the Dissolve tool.

Thanks for any help it is greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
LanceCole
MVP Regular Contributor

noni paulette‌,

Just modify the function as follows.  Make sure your 'UOID' field is set to text and not numeric.

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

View solution in original post

4 Replies
LanceCole
MVP Regular Contributor

noni,

For future reference, when you have a question please post as a question not as a discussion.  You will get a better response from GeoNet.  

This can be easily completed using python in field calculator.  Create a function (uio), such as the following, which has a conditional statement that returns the ClusterID if it is not negative and otherwise returns the SourceID.  You can customize this as needed.

def uio(cluster, source):
  if cluster > 0:
    return cluster
  else:
    return source‍‍‍‍‍

You will call this using the statement uio( !ClusterID!, !SourceID!).

Below is the same shown in Field Calculator from ArcGIS Desktop.  It would be the same in Pro.

nonipaulette
New Contributor II

Lance

Thanks alot for taking the time to resolve the task and the geonet tip. Greatly appreciated!

-Noni

0 Kudos
nonipaulette
New Contributor II

Hi Lance im running into an issue where two fields are populating the new field with the same value. Here is a description below:

Your code worked, however I need to tweak it so that the copied values are uniquely distinguishable.

Thanks again!

0 Kudos
LanceCole
MVP Regular Contributor

noni paulette‌,

Just modify the function as follows.  Make sure your 'UOID' field is set to text and not numeric.

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