# Basic Field Calculator help

I have no experience with Field Calculator in ArcGIS 10.

I have a very simple calculation I would like to perform that I could do easily via pure SQL but just can't seem to get my head around it in the Field Calculator.

Basically I have a table of 5000 weeds of which there are around 10 types. I want to update one field in the table (called CLASS) to be either A, B, or C depending on another field: WEED_NAME

So, if WEED_NAME = 'Hyptis' then Class = 'A' else if WEED_NAME = 'Parkinsonia' then Class = 'B' etc

I know it is very basic, but I just can't seem to get my head around it on this long Wednesday afternoon.

-
For the advanced tasks using Field Calculator, you should need to know a litle bit of programming skill with either VB or Python, at least you should know how to make loop or condition routine. i see ArcGIS Help is a good start with many useful examples. Have you not seen it yet? –  VietThanh Le Mar 28 '12 at 8:49

As a modification of Stev_k's code you may use dictionaries.

``````def weedClassFromName(weedName):
# create dictionary
weedDic = {'Hyptis':'A', 'Parkinsonia':'B'}
# process weedName in dictionary; default is used for undefined
return weedDic.get(weedName,'undefined weed')
``````

On the other hand, as you have loads of weeds in just a few classes, it may be more convenient to use lists.

``````def weedClassFromName(weedName):
if weedName in ['Hyptis', 'another Hyptis', 'yet another Hyptis']:
return 'A'
elif weedName in ['Parkinsonia', 'another Parkinsonia', 'yet another Parkinsonia']:
return 'B'
else:
return 'undefined weed'
``````

In both cases you need the actual code

``````weedClassFromName(!WEED_NAME!)
``````

It should be fairly easy to produce those lists out of your tables using advanced search and replace.

-

It's quite similar to this question, but this code should do the trick:

Pre-logic block:

``````def ifBlock(weedName):
if weedName== 'Hyptis':
return "A"
elif weedName== 'Parkinsonia':
return "B"
elif weedName== 'xxxx':
return "C"
``````

Then in the actual code:

``````ifBlock(!WEED_NAME!)
``````

Make sure you select Python as the script engine

-

You could probably do this as a conditional in the field calculator, but I think it would be quicker to do the following:

In the table window:

Select by Attributes: WEED_NAME = 'Hyptis' Right click and open Field Calculator on "Class" field. In the value window, enter 'A' and press OK.

Repeat this for each combination of WEED_NAME and 'B', then 'C'.

I find sometimes I spend more time trying to find an easier solution to the problem, than I would by doing it the "Hard" way.

Hope this helps!

-
Perhaps for only three fields, but for 300?! Give a man a fish etc. etc. –  Stev_k Mar 28 '12 at 9:04
@Stev_k, I see your point and accept the elegance of your answer compared to mine. I would say that based on the original question, giving a fundamental solution that requires no programming is equally as important as the more advanced python solution. Teaching a man to fish means teaching the basics first, and then building on them, right? –  Get Spatial Mar 28 '12 at 16:19
Point taken, it wasn't meant to be a dig. Each way is equally valid. Overall, I'd say Thomas' solution is the best as it abstracts the whole problem that little bit more. But for those people who can't script, yes your answer is perfect. But they should definitely learn! –  Stev_k Mar 28 '12 at 16:35