2

I have a shapefile with a number of fields. I have one field (let's call it Field A) that I need to populate based on another field (let's call it Field B).

  • If Field B is greater then 0 I need it to populate Field A with the text 'Fill'
  • If Field B is zero then I need it to populate Field A with the text 'NoChange'
  • If Field B is less then 0 I need it to populate Field A with the text 'Drain'

I should know what the Python code for this needs to be.

6

Here is a python solution:

import arcpy

fc = r'C:\path\to\your.gdb\feature_class'

with arcpy.da.UpdateCursor(fc, ["FieldA", "FieldB"]) as cursor:
    for row in cursor:
        if row[1] > 0:
            row[0] = "Fill"
        elif row[1] == 0:
            row[0] = "NoChange"
        elif row[1] < 0:
            row[0] = "Drain"
        cursor.updateRow(row)
2

Calculate Field tool can be used to do this in one step. Very similar to what @Aaron suggests, but as a single gp tool and less code.

Parameters would be

Field Name: Field A

Expression : x(!Field B!)

Expression type : Python

Code Block

def x(val):
    if val > 0:
        return "Fill"
    elif val == 0:
        return "NoChange"
    elif val < 0:
        return "Drain"
1

I would not use python for this.

Select by attributes, everything in Field B > 0. Then use the field calculator to populate Field A with "Fill". Then, select Field B = 0 and use field calculator again to populate Field A with "NoChange". Finally, select Field B < 0 and use field calculator to populate Field B with "Drain".

Is there a reason you want to use Python for this? It seems faster just to do it in the user interface.

  • If she only needs to do it once, sure, doing it via the GUI like you describe might be faster. But not so if she needs to do it many times (e.g., on many such shapefiles). – Paulo Raposo Nov 7 '15 at 10:28
  • She specifically requested Python code. She may or may not have her reasons, but only Aaron's answer addressed the question as it was asked. – Tom Nov 9 '15 at 22:18
  • I would say if you have the time, always use python to do something like this. It will eventually make you quicker and better with python. – Tangnar Jan 19 '16 at 21:22
0

thanks everyone for your help! I just realized I never responded. Sorry about that! I fixed the problem but can't remember now how I did it! Oops.

  • That would make a reasonable comment but doesn't make for a very useful answer. – jbchurchill Jan 19 '16 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.