0

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".

closed as off-topic by Dan C, jbalk, Kadir Şahbaz, user2856, xunilk Oct 4 '18 at 1:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – Dan C, jbalk, user2856, xunilk
If this question can be reworded to fit the rules in the help center, please edit the 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. – id est laborum 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
3

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'
  else:
    return 'NotGood' """


arcpy.CalculateField_management(
    myShapefile,
    fieldToCalc,
    "CalculateValue( !{}! )".format(conditionField),
    "PYTHON",
    codeBlock
)

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"
        else:
            row[0] = "NotGood"
        uc.updateRow(row)
2
def ifthen(a,b):
    if a == 1:
        b = "Good"
    else:
        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.

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