I have two fields, "P_Area" and "PC_Area", whose sizes I'd like to compare in a third field, "Areas_Equal". The "Areas_Equal" field should contain a "Yes" if the other two are equal and a "No" if they aren't.
How can I do this with Field Calculator and its Python Parser?
2 Answers 2
If you're using Python in ArcGIS you may be using an =
for comparison instead of an ==
Your code should be similar to the below, if you use Python:
Pre-Logic Script Code - Check "Show Codeblock"
def ifBlock(pArea,pcArea):
if pArea == pcArea:
return 1
else:
return 0
Then in the actual code:
ifBlock(!P_AreaFieldName!,!PC_AreaFieldName!)
-
6And, of course, if he wants "Yes" and "No" instead of 1 and 0, he should use
return "Yes"
andreturn "No"
.nmpeterson– nmpeterson2012年03月19日 13:44:46 +00:00Commented Mar 19, 2012 at 13:44 -
1Another way to write this in Python (that doesn't require the code block) is this...
1 if !P_Area! == !PC_Area! else 0
Jason Miller– Jason Miller2014年08月12日 02:35:38 +00:00Commented Aug 12, 2014 at 2:35
Please always mention which tool you are using. I will explain how to do this in QGIS because it's free.
Open your shapefile. Open the attributes table. At the bottom create a new columon, call it 'test' or whatever. Now click on advanced search. Type 'P_Area = PC_Area' (without quotes) in SQL where clause. Hit OK.
Now click on 'Open field calculator' (ctrl+I). On the top check 'update existing field' and 'only update selected features'. In field calculator, type '1'. Hit ok.
Now, go back to advanced search, this time search for 'P_Area != PC_Area'. Do same steps as above, except input '0' in field calculator instead of '1'.
I am sure you can do it in a smarter way, but hey, this works. :)
Explore related questions
See similar questions with these tags.