2

I have created a new field called "rank" in the attribute table of my soil data, and I want to populate my new "rank" field with a number of 1 to 5 based on attribute values from another field, for example: "if the attribute in the soil type field is peridotite, i want to fill the rank field with 1. and if the attribute in the soil type field is granodiorite i want to fill the rank field with 2" etc.

How would I go about doing this with field calculator? or is there another way I should be doing this.

  • 1
    Are you updating a raster or a vector layer? You've mentioned both Raster Calculator (updates Rasters) and Field Calculator (updates Vectors). Which is it you want to update? – Midavalo May 8 '17 at 20:49
  • Im sorry, I meant the field Calculater option that can be selected when right clicking the name of the field. – Joshua Aranda May 8 '17 at 20:58
  • Please edit your question and title to correct the reference – Midavalo May 8 '17 at 20:58
4

In the field calculator (on the field you want to populate), select the Python parser at the top, check the Show Codeblock box and modify and use the code below to calculate your field:

def myrank(myfield):
    values = {'Peridotite': 1,
              'Granodiorite': 2,
              'Unobtanium': 3,
              'Somethingelseorite': 4 }

    if myfield in values:
        return values[myfield]

And then in the expression box at the bottom enter:

myrank(!SoilType!)

Update the values in values dictionary where the name is what you're looking for in your Soil Type field and the number is what you want to populate into your Rank field. Change myrank(!SoilType!) to match the field name of your Soil Type field (must be within two ! marks).

  • Nice answer, I was searching for a duplicate question and answer for the seemingly increasingly-common "how to use field calculator to populate a field using a simple if-then" but couldn't find one with a concise answer. – Dan C May 8 '17 at 21:16
  • Worked like a charm Midavalo. Also nice touch with the 'Unobtanium' and 'Somethingelseorite', made me chuckle haha – Joshua Aranda May 8 '17 at 21:19

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.