Select to view content in your preferred language

Python script to field calculate one field based on another field's value

4516
4
Jump to solution
02-05-2015 06:17 AM
SusanO_Connell
Emerging Contributor

I would like to find a python script to field calculate one field based on another field's value.  For example, "Field B" will have a code '23' if "Field A" is equal to CST, or if it is equal to AFF, it will be '11'. Also need to know what to use as a wildcard if CST or AFF is followed by more descriptions.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MahtabAlam1
Frequent Contributor

It will require a code block which is nothing but a python function. Please refer Calculate Field Examples

So all you need is something like

def cal( valA):

    if(valA.startswith('CST') or valA.startswith('AFF')):

        return 11

    else:

        return

and call it as

cal(!Field_A!)

Hope that helps.

View solution in original post

4 Replies
MahtabAlam1
Frequent Contributor

It will require a code block which is nothing but a python function. Please refer Calculate Field Examples

So all you need is something like

def cal( valA):

    if(valA.startswith('CST') or valA.startswith('AFF')):

        return 11

    else:

        return

and call it as

cal(!Field_A!)

Hope that helps.

SusanO_Connell
Emerging Contributor

Thank you very much!

Susan O’Connell

Geographic Information Systems

GIS Specialist

XanderBakker
Esri Esteemed Contributor

Hi Susan O'Connell‌,

If your the solution provided by Mahtab Alam‌ solved your problem (and I think it did), could you please mark his answer as the correct one.

Thank you,

Xander

GeoNet Moderator

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Susan O'Connell,

I think you ment to mark the answer by Mahtab Alam as helpful, right? Since you marked your own response as helpful.

0 Kudos