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.
4 Answers 4
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)
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"
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– Paulo Raposo2015年11月07日 10:28:52 +00:00Commented 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.Tom– Tom2015年11月09日 22:18:54 +00:00Commented 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.Tangnar– Tangnar2016年01月19日 21:22:24 +00:00Commented Jan 19, 2016 at 21:22
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– jbchurchill2016年01月19日 21:30:44 +00:00Commented Jan 19, 2016 at 21:30
Explore related questions
See similar questions with these tags.