On a shapefile, how to populate fields with values based on another field. Like an if condition script. "If field X has value 1 then populate field Y with either this or that values. For example, populate values of either "Good" or "NotGood" in newly formed field based on values from field X. If field X has values 1, then populate newfield with "Good" or if the field x has value like 4 ( or any value other than 1), then populate newfield with "NotGood".

| improve this question | | | | |
  • 1
    Are you using Esri software? Assuming so, is it ArcMap or ArcGIS Pro, and are you using the "Field Calculator" tool or are you trying to write a standalone python script? You can add tags (arcpy, arcgis-desktop, arcgis-pro, etc) to make your question more clear without adding verbosity to the question itself. Also this looks like perhaps a duplicate of the other question you asked last week. Rather than create a new question, perhaps you can go back and edit that one. – Fing Lixon Oct 3 '18 at 18:24
  • Just python scripts. If condition. The example i gave is pretty much what I wanted help with.The result has to be populated in the respective field. – Katappa Oct 3 '18 at 19:03

Assuming you've got access to Esri's arcpy site package, you can use the CalculateField tool. Syntax for that would be something like this . . .

import arcpy

myShapefile = r"C:\temp\myShapefile.shp"
fieldToCalc = "FieldThatsEitherGoodOrNotGood"
conditionField = "FieldThatContainsIntegers"
codeBlock="""def CalculateValue(otherFieldValue):
  if otherFieldValue == 1:
    return 'Good'
    return 'NotGood' """

    "CalculateValue( !{}! )".format(conditionField),

For more flexibility you may look into using an UpdateCursor thusly:

import arcpy

myShapefile = r"C:\temp\myShapefile.shp"
fieldToCalc = "FieldThatsEitherGoodOrNotGood"
conditionField = "FieldThatContainsIntegers"

with arcpy.da.UpdateCursor(myShapefile, [fieldToCalc, conditionField]) as uc:
    for row in uc:
        if row[1] == 1:
            row[0] = "Good"
            row[0] = "NotGood"
| improve this answer | | | | |
def ifthen(a,b):
    if a == 1:
        b = "Good"
        b = "NotGood"
    return b

Here is a simple if then statement. I'd need more info about your problem in order to explain how to implement the if then function.

| improve this answer | | | | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.