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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 7, 2015 at 0:20

4 Answers 4

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)
answered Nov 7, 2015 at 1:03
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"
answered Nov 7, 2015 at 6:42
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.

answered Nov 7, 2015 at 0:29
3
  • 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). Commented Nov 7, 2015 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. Commented Nov 9, 2015 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. Commented Jan 19, 2016 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.

answered Jan 19, 2016 at 21:07
1
  • That would make a reasonable comment but doesn't make for a very useful answer. Commented Jan 19, 2016 at 21:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.