Writing Python code block in field calculator?Null output from field calculator code blockWriting conditional...

What makes accurate emulation of old systems a difficult task?

How exactly does Hawking radiation decrease the mass of black holes?

Why must Chinese maps be obfuscated?

Dynamic SOQL query relationship with field visibility for Users

How could Tony Stark make this in Endgame?

Why does Mind Blank stop the Feeblemind spell?

Critique of timeline aesthetic

What's the name of these pliers?

Pulling the rope with one hand is as heavy as with two hands?

As an international instructor, should I openly talk about my accent?

Check if a string is entirely made of the same substring

What term is being referred to with "reflected-sound-of-underground-spirits"?

Which big number is bigger?

Contradiction proof for inequality of P and NP?

What happened to Captain America in Endgame?

Converting a sprinkler system's 24V AC outputs to 3.3V DC logic inputs

Should the Death Curse affect an undead PC in the Tomb of Annihilation adventure?

How to stop co-workers from teasing me because I know Russian?

infinitely many negative and infinitely many positive numbers

How does Captain America channel this power?

What are the characteristics of a typeless programming language?

How to have a sharp product image?

How to fry ground beef so it is well-browned

"You've called the wrong number" or "You called the wrong number"



Writing Python code block in field calculator?


Null output from field calculator code blockWriting conditional (if/then) statements into ArcGIS Field Calculator using Python Parser?Writing Python script in Field Calculator in ArcGIS Desktop?Python code in ArcGIS field calculator error?Writing expression for field calculator in Python?Using an IF statement in Field CalculatorField Calculator: Simple Python If-then and mathsUsing Python to convert string values with Calculate Field in ArcGIS ModelBuilder?Writing If-Then statement in ArcGIS Pro field calculator using Python?Using IF-Statements in Calculate Field Code Block inside an Iteration






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







6















I am a novice in Python as well has having minimal experience with ArcMap. Currently I am using a U.S. Census TIGER file, mapping roads in Harris County, TX. I want to add a new field called SPEED, which will return a speed associated with the MTFCC string (MTFCC is the column which classifies road type. e.g. S1400, S1200, etc.).



I opened my attributes table, added a new field called SPEED, clicked on the Field Calculator of the speed column, and inserted my own Python code. I made sure to click Python (not VB), and have my return value be a number. My code is as follows...



def SpeedCalc(MTFCC):
if MTFCC == "S1400":
return 25

elif MTFCC == "S1200":
return 45

elif MTFCC == "S1100":
return 65

elif MTFCC == "S1630":
return 25

elif MTFCC == "S1730":
return 15

else:
return 9999


I made sure below where 'SPEED = ' is to have "SpeedCalc(!MTFCC!) written. No errors were thrown, but all my columns were listed as 0. I do not have administrative rights to my working computer or else I would insert my code in to Python first and see what is wrong. As far as I'm aware, my syntax is correct and the statement executes but nothing is done. MTFCC are listed as strings in the properties tab.










share|improve this question




















  • 1





    Perhaps there are leading/trailing spaces, or maybe a case problem (python is case sensitive so 'S000' != 's000') try MTFCC = MTFCC.strip().upper() on the first line of your codeblock, like in stackoverflow.com/questions/1185524/…

    – Michael Stimson
    Nov 5 '17 at 21:42








  • 2





    Do you use SDE geodatabase? if your answer is Yes, do you have edit permission for this layer?

    – wetland
    Nov 5 '17 at 21:44








  • 1





    See if this feature class sits inside environment extent.

    – FelixIP
    Nov 5 '17 at 22:03






  • 1





    Make sure you haven't got a selection in the layer if you're in ArcMap. It is possible to have 0 features selected in a layer, in which case the calculation is done against 0 rows.. but that doesn't sound likely if you're getting 0 calculated into your values. Is your SPEED field numeric? If it is TEXT then you will need to quote the return values. I can see you've managed to avoid all the common traps for field calculations and it's still not working, is there any error messages in your results tab? If SDE and versioned are you in an edit session? Can you manually enter a value for one row?

    – Michael Stimson
    Nov 5 '17 at 22:40











  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.

    – Midavalo
    Nov 6 '17 at 15:24


















6















I am a novice in Python as well has having minimal experience with ArcMap. Currently I am using a U.S. Census TIGER file, mapping roads in Harris County, TX. I want to add a new field called SPEED, which will return a speed associated with the MTFCC string (MTFCC is the column which classifies road type. e.g. S1400, S1200, etc.).



I opened my attributes table, added a new field called SPEED, clicked on the Field Calculator of the speed column, and inserted my own Python code. I made sure to click Python (not VB), and have my return value be a number. My code is as follows...



def SpeedCalc(MTFCC):
if MTFCC == "S1400":
return 25

elif MTFCC == "S1200":
return 45

elif MTFCC == "S1100":
return 65

elif MTFCC == "S1630":
return 25

elif MTFCC == "S1730":
return 15

else:
return 9999


I made sure below where 'SPEED = ' is to have "SpeedCalc(!MTFCC!) written. No errors were thrown, but all my columns were listed as 0. I do not have administrative rights to my working computer or else I would insert my code in to Python first and see what is wrong. As far as I'm aware, my syntax is correct and the statement executes but nothing is done. MTFCC are listed as strings in the properties tab.










share|improve this question




















  • 1





    Perhaps there are leading/trailing spaces, or maybe a case problem (python is case sensitive so 'S000' != 's000') try MTFCC = MTFCC.strip().upper() on the first line of your codeblock, like in stackoverflow.com/questions/1185524/…

    – Michael Stimson
    Nov 5 '17 at 21:42








  • 2





    Do you use SDE geodatabase? if your answer is Yes, do you have edit permission for this layer?

    – wetland
    Nov 5 '17 at 21:44








  • 1





    See if this feature class sits inside environment extent.

    – FelixIP
    Nov 5 '17 at 22:03






  • 1





    Make sure you haven't got a selection in the layer if you're in ArcMap. It is possible to have 0 features selected in a layer, in which case the calculation is done against 0 rows.. but that doesn't sound likely if you're getting 0 calculated into your values. Is your SPEED field numeric? If it is TEXT then you will need to quote the return values. I can see you've managed to avoid all the common traps for field calculations and it's still not working, is there any error messages in your results tab? If SDE and versioned are you in an edit session? Can you manually enter a value for one row?

    – Michael Stimson
    Nov 5 '17 at 22:40











  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.

    – Midavalo
    Nov 6 '17 at 15:24














6












6








6


2






I am a novice in Python as well has having minimal experience with ArcMap. Currently I am using a U.S. Census TIGER file, mapping roads in Harris County, TX. I want to add a new field called SPEED, which will return a speed associated with the MTFCC string (MTFCC is the column which classifies road type. e.g. S1400, S1200, etc.).



I opened my attributes table, added a new field called SPEED, clicked on the Field Calculator of the speed column, and inserted my own Python code. I made sure to click Python (not VB), and have my return value be a number. My code is as follows...



def SpeedCalc(MTFCC):
if MTFCC == "S1400":
return 25

elif MTFCC == "S1200":
return 45

elif MTFCC == "S1100":
return 65

elif MTFCC == "S1630":
return 25

elif MTFCC == "S1730":
return 15

else:
return 9999


I made sure below where 'SPEED = ' is to have "SpeedCalc(!MTFCC!) written. No errors were thrown, but all my columns were listed as 0. I do not have administrative rights to my working computer or else I would insert my code in to Python first and see what is wrong. As far as I'm aware, my syntax is correct and the statement executes but nothing is done. MTFCC are listed as strings in the properties tab.










share|improve this question
















I am a novice in Python as well has having minimal experience with ArcMap. Currently I am using a U.S. Census TIGER file, mapping roads in Harris County, TX. I want to add a new field called SPEED, which will return a speed associated with the MTFCC string (MTFCC is the column which classifies road type. e.g. S1400, S1200, etc.).



I opened my attributes table, added a new field called SPEED, clicked on the Field Calculator of the speed column, and inserted my own Python code. I made sure to click Python (not VB), and have my return value be a number. My code is as follows...



def SpeedCalc(MTFCC):
if MTFCC == "S1400":
return 25

elif MTFCC == "S1200":
return 45

elif MTFCC == "S1100":
return 65

elif MTFCC == "S1630":
return 25

elif MTFCC == "S1730":
return 15

else:
return 9999


I made sure below where 'SPEED = ' is to have "SpeedCalc(!MTFCC!) written. No errors were thrown, but all my columns were listed as 0. I do not have administrative rights to my working computer or else I would insert my code in to Python first and see what is wrong. As far as I'm aware, my syntax is correct and the statement executes but nothing is done. MTFCC are listed as strings in the properties tab.







arcgis-desktop field-calculator python-parser if-else






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 35 mins ago









PolyGeo

54.1k1782247




54.1k1782247










asked Nov 5 '17 at 21:21









DPekDPek

4014




4014








  • 1





    Perhaps there are leading/trailing spaces, or maybe a case problem (python is case sensitive so 'S000' != 's000') try MTFCC = MTFCC.strip().upper() on the first line of your codeblock, like in stackoverflow.com/questions/1185524/…

    – Michael Stimson
    Nov 5 '17 at 21:42








  • 2





    Do you use SDE geodatabase? if your answer is Yes, do you have edit permission for this layer?

    – wetland
    Nov 5 '17 at 21:44








  • 1





    See if this feature class sits inside environment extent.

    – FelixIP
    Nov 5 '17 at 22:03






  • 1





    Make sure you haven't got a selection in the layer if you're in ArcMap. It is possible to have 0 features selected in a layer, in which case the calculation is done against 0 rows.. but that doesn't sound likely if you're getting 0 calculated into your values. Is your SPEED field numeric? If it is TEXT then you will need to quote the return values. I can see you've managed to avoid all the common traps for field calculations and it's still not working, is there any error messages in your results tab? If SDE and versioned are you in an edit session? Can you manually enter a value for one row?

    – Michael Stimson
    Nov 5 '17 at 22:40











  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.

    – Midavalo
    Nov 6 '17 at 15:24














  • 1





    Perhaps there are leading/trailing spaces, or maybe a case problem (python is case sensitive so 'S000' != 's000') try MTFCC = MTFCC.strip().upper() on the first line of your codeblock, like in stackoverflow.com/questions/1185524/…

    – Michael Stimson
    Nov 5 '17 at 21:42








  • 2





    Do you use SDE geodatabase? if your answer is Yes, do you have edit permission for this layer?

    – wetland
    Nov 5 '17 at 21:44








  • 1





    See if this feature class sits inside environment extent.

    – FelixIP
    Nov 5 '17 at 22:03






  • 1





    Make sure you haven't got a selection in the layer if you're in ArcMap. It is possible to have 0 features selected in a layer, in which case the calculation is done against 0 rows.. but that doesn't sound likely if you're getting 0 calculated into your values. Is your SPEED field numeric? If it is TEXT then you will need to quote the return values. I can see you've managed to avoid all the common traps for field calculations and it's still not working, is there any error messages in your results tab? If SDE and versioned are you in an edit session? Can you manually enter a value for one row?

    – Michael Stimson
    Nov 5 '17 at 22:40











  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.

    – Midavalo
    Nov 6 '17 at 15:24








1




1





Perhaps there are leading/trailing spaces, or maybe a case problem (python is case sensitive so 'S000' != 's000') try MTFCC = MTFCC.strip().upper() on the first line of your codeblock, like in stackoverflow.com/questions/1185524/…

– Michael Stimson
Nov 5 '17 at 21:42







Perhaps there are leading/trailing spaces, or maybe a case problem (python is case sensitive so 'S000' != 's000') try MTFCC = MTFCC.strip().upper() on the first line of your codeblock, like in stackoverflow.com/questions/1185524/…

– Michael Stimson
Nov 5 '17 at 21:42






2




2





Do you use SDE geodatabase? if your answer is Yes, do you have edit permission for this layer?

– wetland
Nov 5 '17 at 21:44







Do you use SDE geodatabase? if your answer is Yes, do you have edit permission for this layer?

– wetland
Nov 5 '17 at 21:44






1




1





See if this feature class sits inside environment extent.

– FelixIP
Nov 5 '17 at 22:03





See if this feature class sits inside environment extent.

– FelixIP
Nov 5 '17 at 22:03




1




1





Make sure you haven't got a selection in the layer if you're in ArcMap. It is possible to have 0 features selected in a layer, in which case the calculation is done against 0 rows.. but that doesn't sound likely if you're getting 0 calculated into your values. Is your SPEED field numeric? If it is TEXT then you will need to quote the return values. I can see you've managed to avoid all the common traps for field calculations and it's still not working, is there any error messages in your results tab? If SDE and versioned are you in an edit session? Can you manually enter a value for one row?

– Michael Stimson
Nov 5 '17 at 22:40





Make sure you haven't got a selection in the layer if you're in ArcMap. It is possible to have 0 features selected in a layer, in which case the calculation is done against 0 rows.. but that doesn't sound likely if you're getting 0 calculated into your values. Is your SPEED field numeric? If it is TEXT then you will need to quote the return values. I can see you've managed to avoid all the common traps for field calculations and it's still not working, is there any error messages in your results tab? If SDE and versioned are you in an edit session? Can you manually enter a value for one row?

– Michael Stimson
Nov 5 '17 at 22:40













Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.

– Midavalo
Nov 6 '17 at 15:24





Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.

– Midavalo
Nov 6 '17 at 15:24










1 Answer
1






active

oldest

votes


















14














I have found that sometimes I need to include a single return rather than in each if/elif/else (I do not know why this is the case, but it has happened enough to now be my go to). Try setting a variable and returning just that variable at the end.



Codeblock:



def SpeedCalc(MTFCC):
x = 9999
if MTFCC == "S1400":
x = 25
elif MTFCC == "S1200":
x = 45
elif MTFCC == "S1100":
x = 65
elif MTFCC == "S1630":
x = 25
elif MTFCC == "S1730":
x = 15
return x


Expression:



SpeedCalc(!MTFCC!)


Alternatively create a python dictionary to store your lookup and return values, and return the values from that.



Codeblock:



def SpeedCalc(MTFCC):
myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
x = 9999
if MTFCC in myDict:
x = myDict[MTFCC]
return x


Expression:



SpeedCalc(!MTFCC!)




A user tried to edit the answer with the following - I am including this as an additional option, however in my experience some python functions just don't work right in field calculator (as with the single vs multiple return values above). Also I often prefer to have the additional lines, particularly for those new to python, as it's easier to read and understand what is actually happening in the code when learning.



def SpeedCalc(MTFCC):
myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
return myDict.get(MTFCC, 9999)





share|improve this answer


























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "79"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f260758%2fwriting-python-code-block-in-field-calculator%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    14














    I have found that sometimes I need to include a single return rather than in each if/elif/else (I do not know why this is the case, but it has happened enough to now be my go to). Try setting a variable and returning just that variable at the end.



    Codeblock:



    def SpeedCalc(MTFCC):
    x = 9999
    if MTFCC == "S1400":
    x = 25
    elif MTFCC == "S1200":
    x = 45
    elif MTFCC == "S1100":
    x = 65
    elif MTFCC == "S1630":
    x = 25
    elif MTFCC == "S1730":
    x = 15
    return x


    Expression:



    SpeedCalc(!MTFCC!)


    Alternatively create a python dictionary to store your lookup and return values, and return the values from that.



    Codeblock:



    def SpeedCalc(MTFCC):
    myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
    x = 9999
    if MTFCC in myDict:
    x = myDict[MTFCC]
    return x


    Expression:



    SpeedCalc(!MTFCC!)




    A user tried to edit the answer with the following - I am including this as an additional option, however in my experience some python functions just don't work right in field calculator (as with the single vs multiple return values above). Also I often prefer to have the additional lines, particularly for those new to python, as it's easier to read and understand what is actually happening in the code when learning.



    def SpeedCalc(MTFCC):
    myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
    return myDict.get(MTFCC, 9999)





    share|improve this answer






























      14














      I have found that sometimes I need to include a single return rather than in each if/elif/else (I do not know why this is the case, but it has happened enough to now be my go to). Try setting a variable and returning just that variable at the end.



      Codeblock:



      def SpeedCalc(MTFCC):
      x = 9999
      if MTFCC == "S1400":
      x = 25
      elif MTFCC == "S1200":
      x = 45
      elif MTFCC == "S1100":
      x = 65
      elif MTFCC == "S1630":
      x = 25
      elif MTFCC == "S1730":
      x = 15
      return x


      Expression:



      SpeedCalc(!MTFCC!)


      Alternatively create a python dictionary to store your lookup and return values, and return the values from that.



      Codeblock:



      def SpeedCalc(MTFCC):
      myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
      x = 9999
      if MTFCC in myDict:
      x = myDict[MTFCC]
      return x


      Expression:



      SpeedCalc(!MTFCC!)




      A user tried to edit the answer with the following - I am including this as an additional option, however in my experience some python functions just don't work right in field calculator (as with the single vs multiple return values above). Also I often prefer to have the additional lines, particularly for those new to python, as it's easier to read and understand what is actually happening in the code when learning.



      def SpeedCalc(MTFCC):
      myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
      return myDict.get(MTFCC, 9999)





      share|improve this answer




























        14












        14








        14







        I have found that sometimes I need to include a single return rather than in each if/elif/else (I do not know why this is the case, but it has happened enough to now be my go to). Try setting a variable and returning just that variable at the end.



        Codeblock:



        def SpeedCalc(MTFCC):
        x = 9999
        if MTFCC == "S1400":
        x = 25
        elif MTFCC == "S1200":
        x = 45
        elif MTFCC == "S1100":
        x = 65
        elif MTFCC == "S1630":
        x = 25
        elif MTFCC == "S1730":
        x = 15
        return x


        Expression:



        SpeedCalc(!MTFCC!)


        Alternatively create a python dictionary to store your lookup and return values, and return the values from that.



        Codeblock:



        def SpeedCalc(MTFCC):
        myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
        x = 9999
        if MTFCC in myDict:
        x = myDict[MTFCC]
        return x


        Expression:



        SpeedCalc(!MTFCC!)




        A user tried to edit the answer with the following - I am including this as an additional option, however in my experience some python functions just don't work right in field calculator (as with the single vs multiple return values above). Also I often prefer to have the additional lines, particularly for those new to python, as it's easier to read and understand what is actually happening in the code when learning.



        def SpeedCalc(MTFCC):
        myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
        return myDict.get(MTFCC, 9999)





        share|improve this answer















        I have found that sometimes I need to include a single return rather than in each if/elif/else (I do not know why this is the case, but it has happened enough to now be my go to). Try setting a variable and returning just that variable at the end.



        Codeblock:



        def SpeedCalc(MTFCC):
        x = 9999
        if MTFCC == "S1400":
        x = 25
        elif MTFCC == "S1200":
        x = 45
        elif MTFCC == "S1100":
        x = 65
        elif MTFCC == "S1630":
        x = 25
        elif MTFCC == "S1730":
        x = 15
        return x


        Expression:



        SpeedCalc(!MTFCC!)


        Alternatively create a python dictionary to store your lookup and return values, and return the values from that.



        Codeblock:



        def SpeedCalc(MTFCC):
        myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
        x = 9999
        if MTFCC in myDict:
        x = myDict[MTFCC]
        return x


        Expression:



        SpeedCalc(!MTFCC!)




        A user tried to edit the answer with the following - I am including this as an additional option, however in my experience some python functions just don't work right in field calculator (as with the single vs multiple return values above). Also I often prefer to have the additional lines, particularly for those new to python, as it's easier to read and understand what is actually happening in the code when learning.



        def SpeedCalc(MTFCC):
        myDict = {'S1400': 25, 'S1200': 45, 'S1100': 65, 'S1630': 25, 'S1730': 15}
        return myDict.get(MTFCC, 9999)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 34 mins ago









        PolyGeo

        54.1k1782247




        54.1k1782247










        answered Nov 5 '17 at 22:58









        MidavaloMidavalo

        25.9k53274




        25.9k53274






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f260758%2fwriting-python-code-block-in-field-calculator%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

            Meter-Bus Содержание Параметры шины | Стандартизация |...