Replacing integer values in field using ArcPy for ArcGIS Pro?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












In ArcGIS Pro, to replace a text value in a field using Field Calculator you can use:



!FieldName!.replace("Old Value", "New Value")


However, this does not work on an integer field. For example:



!IntegerFieldName!.replace(1250, 810)


or



!IntegerFieldName!.replace("1250", "810")


Will both produce syntax errors.



So in a Long, Short, or any numeric field, what is the correct way to replace a specific number with another number?



This function will eventually be implemented in a standalone Python script like:



import arcpy
arcpy.management.CalculateField(r"C:Shapefile.shp", "FieldName", "!FieldName!.replace(1250, 810)", "PYTHON3", None)









share|improve this question























  • You could just select by attributes all 1250:s and then Field Calculate to 810
    – BERA
    1 hour ago











  • @BERA I should mention that eventually I'm writing this script to be run outside of ArcGIS Pro. So there'd be no selecting by atributes etc. This would be a pure standalone python code.
    – Theo F
    1 hour ago
















up vote
1
down vote

favorite












In ArcGIS Pro, to replace a text value in a field using Field Calculator you can use:



!FieldName!.replace("Old Value", "New Value")


However, this does not work on an integer field. For example:



!IntegerFieldName!.replace(1250, 810)


or



!IntegerFieldName!.replace("1250", "810")


Will both produce syntax errors.



So in a Long, Short, or any numeric field, what is the correct way to replace a specific number with another number?



This function will eventually be implemented in a standalone Python script like:



import arcpy
arcpy.management.CalculateField(r"C:Shapefile.shp", "FieldName", "!FieldName!.replace(1250, 810)", "PYTHON3", None)









share|improve this question























  • You could just select by attributes all 1250:s and then Field Calculate to 810
    – BERA
    1 hour ago











  • @BERA I should mention that eventually I'm writing this script to be run outside of ArcGIS Pro. So there'd be no selecting by atributes etc. This would be a pure standalone python code.
    – Theo F
    1 hour ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











In ArcGIS Pro, to replace a text value in a field using Field Calculator you can use:



!FieldName!.replace("Old Value", "New Value")


However, this does not work on an integer field. For example:



!IntegerFieldName!.replace(1250, 810)


or



!IntegerFieldName!.replace("1250", "810")


Will both produce syntax errors.



So in a Long, Short, or any numeric field, what is the correct way to replace a specific number with another number?



This function will eventually be implemented in a standalone Python script like:



import arcpy
arcpy.management.CalculateField(r"C:Shapefile.shp", "FieldName", "!FieldName!.replace(1250, 810)", "PYTHON3", None)









share|improve this question















In ArcGIS Pro, to replace a text value in a field using Field Calculator you can use:



!FieldName!.replace("Old Value", "New Value")


However, this does not work on an integer field. For example:



!IntegerFieldName!.replace(1250, 810)


or



!IntegerFieldName!.replace("1250", "810")


Will both produce syntax errors.



So in a Long, Short, or any numeric field, what is the correct way to replace a specific number with another number?



This function will eventually be implemented in a standalone Python script like:



import arcpy
arcpy.management.CalculateField(r"C:Shapefile.shp", "FieldName", "!FieldName!.replace(1250, 810)", "PYTHON3", None)






arcpy field-calculator arcgis-pro






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 48 mins ago









PolyGeo♦

51.9k1777231




51.9k1777231










asked 1 hour ago









Theo F

1679




1679











  • You could just select by attributes all 1250:s and then Field Calculate to 810
    – BERA
    1 hour ago











  • @BERA I should mention that eventually I'm writing this script to be run outside of ArcGIS Pro. So there'd be no selecting by atributes etc. This would be a pure standalone python code.
    – Theo F
    1 hour ago
















  • You could just select by attributes all 1250:s and then Field Calculate to 810
    – BERA
    1 hour ago











  • @BERA I should mention that eventually I'm writing this script to be run outside of ArcGIS Pro. So there'd be no selecting by atributes etc. This would be a pure standalone python code.
    – Theo F
    1 hour ago















You could just select by attributes all 1250:s and then Field Calculate to 810
– BERA
1 hour ago





You could just select by attributes all 1250:s and then Field Calculate to 810
– BERA
1 hour ago













@BERA I should mention that eventually I'm writing this script to be run outside of ArcGIS Pro. So there'd be no selecting by atributes etc. This would be a pure standalone python code.
– Theo F
1 hour ago




@BERA I should mention that eventually I'm writing this script to be run outside of ArcGIS Pro. So there'd be no selecting by atributes etc. This would be a pure standalone python code.
– Theo F
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










As far as I know, replace is a string function (along with e.g. capitalize and rstrip) and will not work on integer fields (hence the syntax error).



You can do this easy with a code block:



def replaceInt(number):
if number == 1250:
return 810
else:
return number


Then run: replaceInt(!IntegerFieldName!)






share|improve this answer






















  • Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
    – Theo F
    1 hour ago










  • I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
    – HavardMoe
    1 hour ago










  • thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
    – Theo F
    1 hour ago






  • 1




    You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
    – HavardMoe
    1 hour ago











  • Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
    – HavardMoe
    1 hour ago

















up vote
1
down vote













As an alternative you can use the da.UpdateCursor. It is like a more versatile and powerful field calculator:



import arcpy

fc = r'C:data.gdbfeatureclass123' #Change to match your data
field_to_update = 'IntegerFieldName' #Change to match your data

with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
for row in cursor:
if row[0] == 1250:
row[0] = 810
cursor.updateRow(row)


If you have many values to change, you can use a dictionary instead of many if-elif-else:



integer_dictionary = 1250:810, 1251:900, 1300:1000 #Add keys and values here
with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
for row in cursor:
if row[0] in integer_dictionary:
row[0] = integer_dictionary[row[0]]
cursor.updateRow(row)





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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f296138%2freplacing-integer-values-in-field-using-arcpy-for-arcgis-pro%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    As far as I know, replace is a string function (along with e.g. capitalize and rstrip) and will not work on integer fields (hence the syntax error).



    You can do this easy with a code block:



    def replaceInt(number):
    if number == 1250:
    return 810
    else:
    return number


    Then run: replaceInt(!IntegerFieldName!)






    share|improve this answer






















    • Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
      – Theo F
      1 hour ago










    • I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
      – HavardMoe
      1 hour ago










    • thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
      – Theo F
      1 hour ago






    • 1




      You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
      – HavardMoe
      1 hour ago











    • Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
      – HavardMoe
      1 hour ago














    up vote
    3
    down vote



    accepted










    As far as I know, replace is a string function (along with e.g. capitalize and rstrip) and will not work on integer fields (hence the syntax error).



    You can do this easy with a code block:



    def replaceInt(number):
    if number == 1250:
    return 810
    else:
    return number


    Then run: replaceInt(!IntegerFieldName!)






    share|improve this answer






















    • Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
      – Theo F
      1 hour ago










    • I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
      – HavardMoe
      1 hour ago










    • thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
      – Theo F
      1 hour ago






    • 1




      You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
      – HavardMoe
      1 hour ago











    • Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
      – HavardMoe
      1 hour ago












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    As far as I know, replace is a string function (along with e.g. capitalize and rstrip) and will not work on integer fields (hence the syntax error).



    You can do this easy with a code block:



    def replaceInt(number):
    if number == 1250:
    return 810
    else:
    return number


    Then run: replaceInt(!IntegerFieldName!)






    share|improve this answer














    As far as I know, replace is a string function (along with e.g. capitalize and rstrip) and will not work on integer fields (hence the syntax error).



    You can do this easy with a code block:



    def replaceInt(number):
    if number == 1250:
    return 810
    else:
    return number


    Then run: replaceInt(!IntegerFieldName!)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 1 hour ago









    HavardMoe

    568310




    568310











    • Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
      – Theo F
      1 hour ago










    • I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
      – HavardMoe
      1 hour ago










    • thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
      – Theo F
      1 hour ago






    • 1




      You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
      – HavardMoe
      1 hour ago











    • Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
      – HavardMoe
      1 hour ago
















    • Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
      – Theo F
      1 hour ago










    • I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
      – HavardMoe
      1 hour ago










    • thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
      – Theo F
      1 hour ago






    • 1




      You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
      – HavardMoe
      1 hour ago











    • Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
      – HavardMoe
      1 hour ago















    Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
    – Theo F
    1 hour ago




    Yes that was my feeling regarding it being a string function. I'm still a bit of an arcpy noob. So what would the code be if I wanted to replace 1250 with 810? Do I need to type in the Code Block area?
    – Theo F
    1 hour ago












    I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
    – HavardMoe
    1 hour ago




    I've edited in a suggestion in the answer above. If there are several replaces they can be added in as elif statements in the same code.
    – HavardMoe
    1 hour ago












    thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
    – Theo F
    1 hour ago




    thanks. that works fine in Field Calculator within the application. However, I'd like to implement this as part of a standalone Python script (see my edited original question). Do you know how I could do this?
    – Theo F
    1 hour ago




    1




    1




    You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
    – HavardMoe
    1 hour ago





    You can use field calculator in arcpy, see here (example 3 shows use with a code block): pro.arcgis.com/en/pro-app/tool-reference/data-management/…
    – HavardMoe
    1 hour ago













    Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
    – HavardMoe
    1 hour ago




    Although... in a standalone script it would be more efficient to use an UpdataCursor, as @BERA mentions above
    – HavardMoe
    1 hour ago












    up vote
    1
    down vote













    As an alternative you can use the da.UpdateCursor. It is like a more versatile and powerful field calculator:



    import arcpy

    fc = r'C:data.gdbfeatureclass123' #Change to match your data
    field_to_update = 'IntegerFieldName' #Change to match your data

    with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
    for row in cursor:
    if row[0] == 1250:
    row[0] = 810
    cursor.updateRow(row)


    If you have many values to change, you can use a dictionary instead of many if-elif-else:



    integer_dictionary = 1250:810, 1251:900, 1300:1000 #Add keys and values here
    with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
    for row in cursor:
    if row[0] in integer_dictionary:
    row[0] = integer_dictionary[row[0]]
    cursor.updateRow(row)





    share|improve this answer


























      up vote
      1
      down vote













      As an alternative you can use the da.UpdateCursor. It is like a more versatile and powerful field calculator:



      import arcpy

      fc = r'C:data.gdbfeatureclass123' #Change to match your data
      field_to_update = 'IntegerFieldName' #Change to match your data

      with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
      for row in cursor:
      if row[0] == 1250:
      row[0] = 810
      cursor.updateRow(row)


      If you have many values to change, you can use a dictionary instead of many if-elif-else:



      integer_dictionary = 1250:810, 1251:900, 1300:1000 #Add keys and values here
      with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
      for row in cursor:
      if row[0] in integer_dictionary:
      row[0] = integer_dictionary[row[0]]
      cursor.updateRow(row)





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        As an alternative you can use the da.UpdateCursor. It is like a more versatile and powerful field calculator:



        import arcpy

        fc = r'C:data.gdbfeatureclass123' #Change to match your data
        field_to_update = 'IntegerFieldName' #Change to match your data

        with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
        for row in cursor:
        if row[0] == 1250:
        row[0] = 810
        cursor.updateRow(row)


        If you have many values to change, you can use a dictionary instead of many if-elif-else:



        integer_dictionary = 1250:810, 1251:900, 1300:1000 #Add keys and values here
        with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
        for row in cursor:
        if row[0] in integer_dictionary:
        row[0] = integer_dictionary[row[0]]
        cursor.updateRow(row)





        share|improve this answer














        As an alternative you can use the da.UpdateCursor. It is like a more versatile and powerful field calculator:



        import arcpy

        fc = r'C:data.gdbfeatureclass123' #Change to match your data
        field_to_update = 'IntegerFieldName' #Change to match your data

        with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
        for row in cursor:
        if row[0] == 1250:
        row[0] = 810
        cursor.updateRow(row)


        If you have many values to change, you can use a dictionary instead of many if-elif-else:



        integer_dictionary = 1250:810, 1251:900, 1300:1000 #Add keys and values here
        with arcpy.da.UpdateCursor(fc,field_to_update) as cursor:
        for row in cursor:
        if row[0] in integer_dictionary:
        row[0] = integer_dictionary[row[0]]
        cursor.updateRow(row)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 3 mins ago

























        answered 19 mins ago









        BERA

        11.4k41536




        11.4k41536



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f296138%2freplacing-integer-values-in-field-using-arcpy-for-arcgis-pro%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery