Copying/replacing first letter in column of attribute table in ArcMap with field calculator and Python...





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







0















How to copy / replace the first letter in a column if that letter is "N" and replace with "C" in the field name "DISTRICT" inside the attribute table in ArcMap using Python inside the field calculator?










share|improve this question















put on hold as off-topic by Dan C, Kadir Şahbaz, csk, BERA, ahmadhanb 14 mins ago


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, Kadir Şahbaz, csk, BERA, ahmadhanb

If this question can be reworded to fit the rules in the help center, please edit the question.























    0















    How to copy / replace the first letter in a column if that letter is "N" and replace with "C" in the field name "DISTRICT" inside the attribute table in ArcMap using Python inside the field calculator?










    share|improve this question















    put on hold as off-topic by Dan C, Kadir Şahbaz, csk, BERA, ahmadhanb 14 mins ago


    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, Kadir Şahbaz, csk, BERA, ahmadhanb

    If this question can be reworded to fit the rules in the help center, please edit the question.



















      0












      0








      0








      How to copy / replace the first letter in a column if that letter is "N" and replace with "C" in the field name "DISTRICT" inside the attribute table in ArcMap using Python inside the field calculator?










      share|improve this question
















      How to copy / replace the first letter in a column if that letter is "N" and replace with "C" in the field name "DISTRICT" inside the attribute table in ArcMap using Python inside the field calculator?







      arcgis-desktop arcmap field-calculator python-parser






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 5 hours ago









      PolyGeo

      53.9k1781245




      53.9k1781245










      asked 9 hours ago









      Anthony StokesAnthony Stokes

      947




      947




      put on hold as off-topic by Dan C, Kadir Şahbaz, csk, BERA, ahmadhanb 14 mins ago


      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, Kadir Şahbaz, csk, BERA, ahmadhanb

      If this question can be reworded to fit the rules in the help center, please edit the question.







      put on hold as off-topic by Dan C, Kadir Şahbaz, csk, BERA, ahmadhanb 14 mins ago


      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, Kadir Şahbaz, csk, BERA, ahmadhanb

      If this question can be reworded to fit the rules in the help center, please edit the question.






















          4 Answers
          4






          active

          oldest

          votes


















          4














          The general format for overall string replace in Python in the Field Calculator is



          = !stringvar!.replace("substring to find", "new substring")


          If you want to only change the first initial for all records, you could build this based on a slice of the string.



          = "C" + !stringvar![1:]


          If you only want to change the first initial if it starts with a "N", then you're getting into conditional statements (if/then) and should wrap this in a function for use within the Field Calculator. Build this in the codebook/pre-Logic script code.



          def replaceIfN(fieldtochange):
          if fieldtochange.lower().startswith("n"): # handles both n and N
          return "C" + fieldtochange[1:]
          else: # no change made
          return fieldtochange


          Run this with



           = replaceIfN(!DISTRICT!) 


          See http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm






          share|improve this answer
























          • The conditional statement is the solution I was looking for.

            – Anthony Stokes
            8 hours ago



















          6














          You don't even have to write any code!



          Simply edit the table and do a find and replace on the selected field.



          Find and replace



          Example of replacing B with XXX.



          Replaced



          Result of replacement






          share|improve this answer
























          • I need to know how to copy / replace the first letter in a column if that letter is "N" and replace with "C" using python inside the field calculator. I don't want to replace every instance of "N". This would require some code.

            – Anthony Stokes
            8 hours ago






          • 1





            Well... if you look at the image, what does Text match say... You can use @Jackson_Dunn's approach if you intend to wrap the field calculate in say modelbuilder. If you just want to replace the first N with C then my approach is less painful and quicker.

            – Hornbydd
            8 hours ago













          • I see what you mean. This is the fastest way without code but I like to always know the python equivalent.

            – Anthony Stokes
            8 hours ago













          • Are imgur images always going to be on the site, or is there a risk of them not loading at some distant point in the future? If the latter, please elaborate on your answer (e.g. Select "Start of Field" for Text match)

            – smiller
            7 hours ago






          • 1





            I didnt know of find and replace, nice!

            – BERA
            6 hours ago



















          1














          You can add a number to the python replace code to determine how many instances to change (in your case just one):



          !District!.replace("C", "N", 1)





          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





















          • Thanks @Jackson Dunn. I'd be curious as to performance between the string replace number of instances vs. string slicing. I suspect in this case it's minimal but may be worth testing on larger fields or more complex replacements.

            – smiller
            5 hours ago



















          0














          Using vbscript instead if you're working in ArcGIS Desktop, The code is:



          REPLACE ([DISTRICT],"C","N",1,1)


          Where :




          • the first 1 equals the line position (first character)

          • and the second 1 is amount of characters to change (only need to change one letter per row, not all C's and N's).






          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





















          • This is the VB script solution so thank you for that but I was wondering how to solve it using python in the field calculator.

            – Anthony Stokes
            9 hours ago


















          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          The general format for overall string replace in Python in the Field Calculator is



          = !stringvar!.replace("substring to find", "new substring")


          If you want to only change the first initial for all records, you could build this based on a slice of the string.



          = "C" + !stringvar![1:]


          If you only want to change the first initial if it starts with a "N", then you're getting into conditional statements (if/then) and should wrap this in a function for use within the Field Calculator. Build this in the codebook/pre-Logic script code.



          def replaceIfN(fieldtochange):
          if fieldtochange.lower().startswith("n"): # handles both n and N
          return "C" + fieldtochange[1:]
          else: # no change made
          return fieldtochange


          Run this with



           = replaceIfN(!DISTRICT!) 


          See http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm






          share|improve this answer
























          • The conditional statement is the solution I was looking for.

            – Anthony Stokes
            8 hours ago
















          4














          The general format for overall string replace in Python in the Field Calculator is



          = !stringvar!.replace("substring to find", "new substring")


          If you want to only change the first initial for all records, you could build this based on a slice of the string.



          = "C" + !stringvar![1:]


          If you only want to change the first initial if it starts with a "N", then you're getting into conditional statements (if/then) and should wrap this in a function for use within the Field Calculator. Build this in the codebook/pre-Logic script code.



          def replaceIfN(fieldtochange):
          if fieldtochange.lower().startswith("n"): # handles both n and N
          return "C" + fieldtochange[1:]
          else: # no change made
          return fieldtochange


          Run this with



           = replaceIfN(!DISTRICT!) 


          See http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm






          share|improve this answer
























          • The conditional statement is the solution I was looking for.

            – Anthony Stokes
            8 hours ago














          4












          4








          4







          The general format for overall string replace in Python in the Field Calculator is



          = !stringvar!.replace("substring to find", "new substring")


          If you want to only change the first initial for all records, you could build this based on a slice of the string.



          = "C" + !stringvar![1:]


          If you only want to change the first initial if it starts with a "N", then you're getting into conditional statements (if/then) and should wrap this in a function for use within the Field Calculator. Build this in the codebook/pre-Logic script code.



          def replaceIfN(fieldtochange):
          if fieldtochange.lower().startswith("n"): # handles both n and N
          return "C" + fieldtochange[1:]
          else: # no change made
          return fieldtochange


          Run this with



           = replaceIfN(!DISTRICT!) 


          See http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm






          share|improve this answer













          The general format for overall string replace in Python in the Field Calculator is



          = !stringvar!.replace("substring to find", "new substring")


          If you want to only change the first initial for all records, you could build this based on a slice of the string.



          = "C" + !stringvar![1:]


          If you only want to change the first initial if it starts with a "N", then you're getting into conditional statements (if/then) and should wrap this in a function for use within the Field Calculator. Build this in the codebook/pre-Logic script code.



          def replaceIfN(fieldtochange):
          if fieldtochange.lower().startswith("n"): # handles both n and N
          return "C" + fieldtochange[1:]
          else: # no change made
          return fieldtochange


          Run this with



           = replaceIfN(!DISTRICT!) 


          See http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 9 hours ago









          smillersmiller

          2,229217




          2,229217













          • The conditional statement is the solution I was looking for.

            – Anthony Stokes
            8 hours ago



















          • The conditional statement is the solution I was looking for.

            – Anthony Stokes
            8 hours ago

















          The conditional statement is the solution I was looking for.

          – Anthony Stokes
          8 hours ago





          The conditional statement is the solution I was looking for.

          – Anthony Stokes
          8 hours ago













          6














          You don't even have to write any code!



          Simply edit the table and do a find and replace on the selected field.



          Find and replace



          Example of replacing B with XXX.



          Replaced



          Result of replacement






          share|improve this answer
























          • I need to know how to copy / replace the first letter in a column if that letter is "N" and replace with "C" using python inside the field calculator. I don't want to replace every instance of "N". This would require some code.

            – Anthony Stokes
            8 hours ago






          • 1





            Well... if you look at the image, what does Text match say... You can use @Jackson_Dunn's approach if you intend to wrap the field calculate in say modelbuilder. If you just want to replace the first N with C then my approach is less painful and quicker.

            – Hornbydd
            8 hours ago













          • I see what you mean. This is the fastest way without code but I like to always know the python equivalent.

            – Anthony Stokes
            8 hours ago













          • Are imgur images always going to be on the site, or is there a risk of them not loading at some distant point in the future? If the latter, please elaborate on your answer (e.g. Select "Start of Field" for Text match)

            – smiller
            7 hours ago






          • 1





            I didnt know of find and replace, nice!

            – BERA
            6 hours ago
















          6














          You don't even have to write any code!



          Simply edit the table and do a find and replace on the selected field.



          Find and replace



          Example of replacing B with XXX.



          Replaced



          Result of replacement






          share|improve this answer
























          • I need to know how to copy / replace the first letter in a column if that letter is "N" and replace with "C" using python inside the field calculator. I don't want to replace every instance of "N". This would require some code.

            – Anthony Stokes
            8 hours ago






          • 1





            Well... if you look at the image, what does Text match say... You can use @Jackson_Dunn's approach if you intend to wrap the field calculate in say modelbuilder. If you just want to replace the first N with C then my approach is less painful and quicker.

            – Hornbydd
            8 hours ago













          • I see what you mean. This is the fastest way without code but I like to always know the python equivalent.

            – Anthony Stokes
            8 hours ago













          • Are imgur images always going to be on the site, or is there a risk of them not loading at some distant point in the future? If the latter, please elaborate on your answer (e.g. Select "Start of Field" for Text match)

            – smiller
            7 hours ago






          • 1





            I didnt know of find and replace, nice!

            – BERA
            6 hours ago














          6












          6








          6







          You don't even have to write any code!



          Simply edit the table and do a find and replace on the selected field.



          Find and replace



          Example of replacing B with XXX.



          Replaced



          Result of replacement






          share|improve this answer













          You don't even have to write any code!



          Simply edit the table and do a find and replace on the selected field.



          Find and replace



          Example of replacing B with XXX.



          Replaced



          Result of replacement







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 9 hours ago









          HornbyddHornbydd

          27.1k32957




          27.1k32957













          • I need to know how to copy / replace the first letter in a column if that letter is "N" and replace with "C" using python inside the field calculator. I don't want to replace every instance of "N". This would require some code.

            – Anthony Stokes
            8 hours ago






          • 1





            Well... if you look at the image, what does Text match say... You can use @Jackson_Dunn's approach if you intend to wrap the field calculate in say modelbuilder. If you just want to replace the first N with C then my approach is less painful and quicker.

            – Hornbydd
            8 hours ago













          • I see what you mean. This is the fastest way without code but I like to always know the python equivalent.

            – Anthony Stokes
            8 hours ago













          • Are imgur images always going to be on the site, or is there a risk of them not loading at some distant point in the future? If the latter, please elaborate on your answer (e.g. Select "Start of Field" for Text match)

            – smiller
            7 hours ago






          • 1





            I didnt know of find and replace, nice!

            – BERA
            6 hours ago



















          • I need to know how to copy / replace the first letter in a column if that letter is "N" and replace with "C" using python inside the field calculator. I don't want to replace every instance of "N". This would require some code.

            – Anthony Stokes
            8 hours ago






          • 1





            Well... if you look at the image, what does Text match say... You can use @Jackson_Dunn's approach if you intend to wrap the field calculate in say modelbuilder. If you just want to replace the first N with C then my approach is less painful and quicker.

            – Hornbydd
            8 hours ago













          • I see what you mean. This is the fastest way without code but I like to always know the python equivalent.

            – Anthony Stokes
            8 hours ago













          • Are imgur images always going to be on the site, or is there a risk of them not loading at some distant point in the future? If the latter, please elaborate on your answer (e.g. Select "Start of Field" for Text match)

            – smiller
            7 hours ago






          • 1





            I didnt know of find and replace, nice!

            – BERA
            6 hours ago

















          I need to know how to copy / replace the first letter in a column if that letter is "N" and replace with "C" using python inside the field calculator. I don't want to replace every instance of "N". This would require some code.

          – Anthony Stokes
          8 hours ago





          I need to know how to copy / replace the first letter in a column if that letter is "N" and replace with "C" using python inside the field calculator. I don't want to replace every instance of "N". This would require some code.

          – Anthony Stokes
          8 hours ago




          1




          1





          Well... if you look at the image, what does Text match say... You can use @Jackson_Dunn's approach if you intend to wrap the field calculate in say modelbuilder. If you just want to replace the first N with C then my approach is less painful and quicker.

          – Hornbydd
          8 hours ago







          Well... if you look at the image, what does Text match say... You can use @Jackson_Dunn's approach if you intend to wrap the field calculate in say modelbuilder. If you just want to replace the first N with C then my approach is less painful and quicker.

          – Hornbydd
          8 hours ago















          I see what you mean. This is the fastest way without code but I like to always know the python equivalent.

          – Anthony Stokes
          8 hours ago







          I see what you mean. This is the fastest way without code but I like to always know the python equivalent.

          – Anthony Stokes
          8 hours ago















          Are imgur images always going to be on the site, or is there a risk of them not loading at some distant point in the future? If the latter, please elaborate on your answer (e.g. Select "Start of Field" for Text match)

          – smiller
          7 hours ago





          Are imgur images always going to be on the site, or is there a risk of them not loading at some distant point in the future? If the latter, please elaborate on your answer (e.g. Select "Start of Field" for Text match)

          – smiller
          7 hours ago




          1




          1





          I didnt know of find and replace, nice!

          – BERA
          6 hours ago





          I didnt know of find and replace, nice!

          – BERA
          6 hours ago











          1














          You can add a number to the python replace code to determine how many instances to change (in your case just one):



          !District!.replace("C", "N", 1)





          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





















          • Thanks @Jackson Dunn. I'd be curious as to performance between the string replace number of instances vs. string slicing. I suspect in this case it's minimal but may be worth testing on larger fields or more complex replacements.

            – smiller
            5 hours ago
















          1














          You can add a number to the python replace code to determine how many instances to change (in your case just one):



          !District!.replace("C", "N", 1)





          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





















          • Thanks @Jackson Dunn. I'd be curious as to performance between the string replace number of instances vs. string slicing. I suspect in this case it's minimal but may be worth testing on larger fields or more complex replacements.

            – smiller
            5 hours ago














          1












          1








          1







          You can add a number to the python replace code to determine how many instances to change (in your case just one):



          !District!.replace("C", "N", 1)





          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.










          You can add a number to the python replace code to determine how many instances to change (in your case just one):



          !District!.replace("C", "N", 1)






          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited 5 hours ago









          PolyGeo

          53.9k1781245




          53.9k1781245






          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 6 hours ago









          Jackson DunnJackson Dunn

          192




          192




          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.













          • Thanks @Jackson Dunn. I'd be curious as to performance between the string replace number of instances vs. string slicing. I suspect in this case it's minimal but may be worth testing on larger fields or more complex replacements.

            – smiller
            5 hours ago



















          • Thanks @Jackson Dunn. I'd be curious as to performance between the string replace number of instances vs. string slicing. I suspect in this case it's minimal but may be worth testing on larger fields or more complex replacements.

            – smiller
            5 hours ago

















          Thanks @Jackson Dunn. I'd be curious as to performance between the string replace number of instances vs. string slicing. I suspect in this case it's minimal but may be worth testing on larger fields or more complex replacements.

          – smiller
          5 hours ago





          Thanks @Jackson Dunn. I'd be curious as to performance between the string replace number of instances vs. string slicing. I suspect in this case it's minimal but may be worth testing on larger fields or more complex replacements.

          – smiller
          5 hours ago











          0














          Using vbscript instead if you're working in ArcGIS Desktop, The code is:



          REPLACE ([DISTRICT],"C","N",1,1)


          Where :




          • the first 1 equals the line position (first character)

          • and the second 1 is amount of characters to change (only need to change one letter per row, not all C's and N's).






          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





















          • This is the VB script solution so thank you for that but I was wondering how to solve it using python in the field calculator.

            – Anthony Stokes
            9 hours ago
















          0














          Using vbscript instead if you're working in ArcGIS Desktop, The code is:



          REPLACE ([DISTRICT],"C","N",1,1)


          Where :




          • the first 1 equals the line position (first character)

          • and the second 1 is amount of characters to change (only need to change one letter per row, not all C's and N's).






          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





















          • This is the VB script solution so thank you for that but I was wondering how to solve it using python in the field calculator.

            – Anthony Stokes
            9 hours ago














          0












          0








          0







          Using vbscript instead if you're working in ArcGIS Desktop, The code is:



          REPLACE ([DISTRICT],"C","N",1,1)


          Where :




          • the first 1 equals the line position (first character)

          • and the second 1 is amount of characters to change (only need to change one letter per row, not all C's and N's).






          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.










          Using vbscript instead if you're working in ArcGIS Desktop, The code is:



          REPLACE ([DISTRICT],"C","N",1,1)


          Where :




          • the first 1 equals the line position (first character)

          • and the second 1 is amount of characters to change (only need to change one letter per row, not all C's and N's).







          share|improve this answer










          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited 5 hours ago









          PolyGeo

          53.9k1781245




          53.9k1781245






          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 9 hours ago









          Jackson DunnJackson Dunn

          192




          192




          New contributor




          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Jackson Dunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.













          • This is the VB script solution so thank you for that but I was wondering how to solve it using python in the field calculator.

            – Anthony Stokes
            9 hours ago



















          • This is the VB script solution so thank you for that but I was wondering how to solve it using python in the field calculator.

            – Anthony Stokes
            9 hours ago

















          This is the VB script solution so thank you for that but I was wondering how to solve it using python in the field calculator.

          – Anthony Stokes
          9 hours ago





          This is the VB script solution so thank you for that but I was wondering how to solve it using python in the field calculator.

          – Anthony Stokes
          9 hours ago



          Popular posts from this blog

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto