Creating new field in ArcGIS Desktop which contains values from two different columns with certain...











up vote
1
down vote

favorite












Suppose I have a attribute table as shown in the image below:



enter image description here



I have columns A and B with common FID. I would like to add a new column C, which contains the values of column A with the condition that if the value is equal to 25 in that column, then for those rows, the value should be assigned from column B.



The highlighted green color column C in the image is how the result should look like.



Is there a tool to do this or does an expression have to be used?










share|improve this question




























    up vote
    1
    down vote

    favorite












    Suppose I have a attribute table as shown in the image below:



    enter image description here



    I have columns A and B with common FID. I would like to add a new column C, which contains the values of column A with the condition that if the value is equal to 25 in that column, then for those rows, the value should be assigned from column B.



    The highlighted green color column C in the image is how the result should look like.



    Is there a tool to do this or does an expression have to be used?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Suppose I have a attribute table as shown in the image below:



      enter image description here



      I have columns A and B with common FID. I would like to add a new column C, which contains the values of column A with the condition that if the value is equal to 25 in that column, then for those rows, the value should be assigned from column B.



      The highlighted green color column C in the image is how the result should look like.



      Is there a tool to do this or does an expression have to be used?










      share|improve this question















      Suppose I have a attribute table as shown in the image below:



      enter image description here



      I have columns A and B with common FID. I would like to add a new column C, which contains the values of column A with the condition that if the value is equal to 25 in that column, then for those rows, the value should be assigned from column B.



      The highlighted green color column C in the image is how the result should look like.



      Is there a tool to do this or does an expression have to be used?







      arcgis-desktop attribute-table






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 22 hours ago









      Vince

      14.3k32646




      14.3k32646










      asked Dec 5 at 6:07









      nish

      82




      82






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You can use Field Calculator using Python Parser:



          enter image description here



          in the Pre-Logic Script write the following code:



          def getValue(f1,f2):
          if f1 == 25:
          return f2
          else:
          return f1


          C=



          getValue(!A! , !B!)





          share|improve this answer




























            up vote
            6
            down vote













            Python has ternary conditional operator:



            !B! if !A! == 25 else !A!





            share|improve this answer





















            • @nish, to get more information about a shortcut conditional expression, please check this link.
              – Taras
              Dec 5 at 6:51








            • 1




              Simplest the best, no pre-logic script, no argument ordering.
              – fatih_dur
              Dec 5 at 8:03


















            up vote
            3
            down vote













            You can use Field Calculator (see examples here) or the da.UpdateCursor like below. Change input and field names and execute in the python window of ArcMap.



            import arcpy

            fc = r'C:data.gdbfeature_class' #Change to match your data
            fields = ['A','B','C'] #Change to match your data

            with arcpy.da.UpdateCursor(fc,fields) as cursor:
            for row in cursor:
            if row[0] == 25:
            row[2] = row[1]
            else:
            row[2] = row[0]
            cursor.updateRow(row)





            share|improve this answer



















            • 1




              Thank you. But i wasn't sure where to input this code.
              – nish
              Dec 5 at 6:43






            • 1




              @BERA, have you ever used any GIS with GUI? =)
              – Taras
              Dec 5 at 7:05






            • 1




              @BERA, P.S. as always a perfect answer
              – Taras
              Dec 5 at 8:14


















            up vote
            2
            down vote













            In the Field Calculator with a usage of Python parser, please type



            Pre-Logic Script Code:



            def CalcColumn(fieldA, fieldB):
            if fieldA == 25:
            return fieldB
            else:
            return fieldA


            C =



            CalcColumn(!A!, !B!)


            References:




            • Basic If/Then in Python Parser of ArcGIS Field Calculator?

            • Python script for if/elif condition in field calculator






            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: 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%2f305018%2fcreating-new-field-in-arcgis-desktop-which-contains-values-from-two-different-co%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              You can use Field Calculator using Python Parser:



              enter image description here



              in the Pre-Logic Script write the following code:



              def getValue(f1,f2):
              if f1 == 25:
              return f2
              else:
              return f1


              C=



              getValue(!A! , !B!)





              share|improve this answer

























                up vote
                3
                down vote



                accepted










                You can use Field Calculator using Python Parser:



                enter image description here



                in the Pre-Logic Script write the following code:



                def getValue(f1,f2):
                if f1 == 25:
                return f2
                else:
                return f1


                C=



                getValue(!A! , !B!)





                share|improve this answer























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  You can use Field Calculator using Python Parser:



                  enter image description here



                  in the Pre-Logic Script write the following code:



                  def getValue(f1,f2):
                  if f1 == 25:
                  return f2
                  else:
                  return f1


                  C=



                  getValue(!A! , !B!)





                  share|improve this answer












                  You can use Field Calculator using Python Parser:



                  enter image description here



                  in the Pre-Logic Script write the following code:



                  def getValue(f1,f2):
                  if f1 == 25:
                  return f2
                  else:
                  return f1


                  C=



                  getValue(!A! , !B!)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 5 at 6:23









                  ahmadhanb

                  21.4k31951




                  21.4k31951
























                      up vote
                      6
                      down vote













                      Python has ternary conditional operator:



                      !B! if !A! == 25 else !A!





                      share|improve this answer





















                      • @nish, to get more information about a shortcut conditional expression, please check this link.
                        – Taras
                        Dec 5 at 6:51








                      • 1




                        Simplest the best, no pre-logic script, no argument ordering.
                        – fatih_dur
                        Dec 5 at 8:03















                      up vote
                      6
                      down vote













                      Python has ternary conditional operator:



                      !B! if !A! == 25 else !A!





                      share|improve this answer





















                      • @nish, to get more information about a shortcut conditional expression, please check this link.
                        – Taras
                        Dec 5 at 6:51








                      • 1




                        Simplest the best, no pre-logic script, no argument ordering.
                        – fatih_dur
                        Dec 5 at 8:03













                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      Python has ternary conditional operator:



                      !B! if !A! == 25 else !A!





                      share|improve this answer












                      Python has ternary conditional operator:



                      !B! if !A! == 25 else !A!






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 5 at 6:48









                      FelixIP

                      15.9k11441




                      15.9k11441












                      • @nish, to get more information about a shortcut conditional expression, please check this link.
                        – Taras
                        Dec 5 at 6:51








                      • 1




                        Simplest the best, no pre-logic script, no argument ordering.
                        – fatih_dur
                        Dec 5 at 8:03


















                      • @nish, to get more information about a shortcut conditional expression, please check this link.
                        – Taras
                        Dec 5 at 6:51








                      • 1




                        Simplest the best, no pre-logic script, no argument ordering.
                        – fatih_dur
                        Dec 5 at 8:03
















                      @nish, to get more information about a shortcut conditional expression, please check this link.
                      – Taras
                      Dec 5 at 6:51






                      @nish, to get more information about a shortcut conditional expression, please check this link.
                      – Taras
                      Dec 5 at 6:51






                      1




                      1




                      Simplest the best, no pre-logic script, no argument ordering.
                      – fatih_dur
                      Dec 5 at 8:03




                      Simplest the best, no pre-logic script, no argument ordering.
                      – fatih_dur
                      Dec 5 at 8:03










                      up vote
                      3
                      down vote













                      You can use Field Calculator (see examples here) or the da.UpdateCursor like below. Change input and field names and execute in the python window of ArcMap.



                      import arcpy

                      fc = r'C:data.gdbfeature_class' #Change to match your data
                      fields = ['A','B','C'] #Change to match your data

                      with arcpy.da.UpdateCursor(fc,fields) as cursor:
                      for row in cursor:
                      if row[0] == 25:
                      row[2] = row[1]
                      else:
                      row[2] = row[0]
                      cursor.updateRow(row)





                      share|improve this answer



















                      • 1




                        Thank you. But i wasn't sure where to input this code.
                        – nish
                        Dec 5 at 6:43






                      • 1




                        @BERA, have you ever used any GIS with GUI? =)
                        – Taras
                        Dec 5 at 7:05






                      • 1




                        @BERA, P.S. as always a perfect answer
                        – Taras
                        Dec 5 at 8:14















                      up vote
                      3
                      down vote













                      You can use Field Calculator (see examples here) or the da.UpdateCursor like below. Change input and field names and execute in the python window of ArcMap.



                      import arcpy

                      fc = r'C:data.gdbfeature_class' #Change to match your data
                      fields = ['A','B','C'] #Change to match your data

                      with arcpy.da.UpdateCursor(fc,fields) as cursor:
                      for row in cursor:
                      if row[0] == 25:
                      row[2] = row[1]
                      else:
                      row[2] = row[0]
                      cursor.updateRow(row)





                      share|improve this answer



















                      • 1




                        Thank you. But i wasn't sure where to input this code.
                        – nish
                        Dec 5 at 6:43






                      • 1




                        @BERA, have you ever used any GIS with GUI? =)
                        – Taras
                        Dec 5 at 7:05






                      • 1




                        @BERA, P.S. as always a perfect answer
                        – Taras
                        Dec 5 at 8:14













                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      You can use Field Calculator (see examples here) or the da.UpdateCursor like below. Change input and field names and execute in the python window of ArcMap.



                      import arcpy

                      fc = r'C:data.gdbfeature_class' #Change to match your data
                      fields = ['A','B','C'] #Change to match your data

                      with arcpy.da.UpdateCursor(fc,fields) as cursor:
                      for row in cursor:
                      if row[0] == 25:
                      row[2] = row[1]
                      else:
                      row[2] = row[0]
                      cursor.updateRow(row)





                      share|improve this answer














                      You can use Field Calculator (see examples here) or the da.UpdateCursor like below. Change input and field names and execute in the python window of ArcMap.



                      import arcpy

                      fc = r'C:data.gdbfeature_class' #Change to match your data
                      fields = ['A','B','C'] #Change to match your data

                      with arcpy.da.UpdateCursor(fc,fields) as cursor:
                      for row in cursor:
                      if row[0] == 25:
                      row[2] = row[1]
                      else:
                      row[2] = row[0]
                      cursor.updateRow(row)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 5 at 6:45

























                      answered Dec 5 at 6:14









                      BERA

                      14.2k51839




                      14.2k51839








                      • 1




                        Thank you. But i wasn't sure where to input this code.
                        – nish
                        Dec 5 at 6:43






                      • 1




                        @BERA, have you ever used any GIS with GUI? =)
                        – Taras
                        Dec 5 at 7:05






                      • 1




                        @BERA, P.S. as always a perfect answer
                        – Taras
                        Dec 5 at 8:14














                      • 1




                        Thank you. But i wasn't sure where to input this code.
                        – nish
                        Dec 5 at 6:43






                      • 1




                        @BERA, have you ever used any GIS with GUI? =)
                        – Taras
                        Dec 5 at 7:05






                      • 1




                        @BERA, P.S. as always a perfect answer
                        – Taras
                        Dec 5 at 8:14








                      1




                      1




                      Thank you. But i wasn't sure where to input this code.
                      – nish
                      Dec 5 at 6:43




                      Thank you. But i wasn't sure where to input this code.
                      – nish
                      Dec 5 at 6:43




                      1




                      1




                      @BERA, have you ever used any GIS with GUI? =)
                      – Taras
                      Dec 5 at 7:05




                      @BERA, have you ever used any GIS with GUI? =)
                      – Taras
                      Dec 5 at 7:05




                      1




                      1




                      @BERA, P.S. as always a perfect answer
                      – Taras
                      Dec 5 at 8:14




                      @BERA, P.S. as always a perfect answer
                      – Taras
                      Dec 5 at 8:14










                      up vote
                      2
                      down vote













                      In the Field Calculator with a usage of Python parser, please type



                      Pre-Logic Script Code:



                      def CalcColumn(fieldA, fieldB):
                      if fieldA == 25:
                      return fieldB
                      else:
                      return fieldA


                      C =



                      CalcColumn(!A!, !B!)


                      References:




                      • Basic If/Then in Python Parser of ArcGIS Field Calculator?

                      • Python script for if/elif condition in field calculator






                      share|improve this answer

























                        up vote
                        2
                        down vote













                        In the Field Calculator with a usage of Python parser, please type



                        Pre-Logic Script Code:



                        def CalcColumn(fieldA, fieldB):
                        if fieldA == 25:
                        return fieldB
                        else:
                        return fieldA


                        C =



                        CalcColumn(!A!, !B!)


                        References:




                        • Basic If/Then in Python Parser of ArcGIS Field Calculator?

                        • Python script for if/elif condition in field calculator






                        share|improve this answer























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          In the Field Calculator with a usage of Python parser, please type



                          Pre-Logic Script Code:



                          def CalcColumn(fieldA, fieldB):
                          if fieldA == 25:
                          return fieldB
                          else:
                          return fieldA


                          C =



                          CalcColumn(!A!, !B!)


                          References:




                          • Basic If/Then in Python Parser of ArcGIS Field Calculator?

                          • Python script for if/elif condition in field calculator






                          share|improve this answer












                          In the Field Calculator with a usage of Python parser, please type



                          Pre-Logic Script Code:



                          def CalcColumn(fieldA, fieldB):
                          if fieldA == 25:
                          return fieldB
                          else:
                          return fieldA


                          C =



                          CalcColumn(!A!, !B!)


                          References:




                          • Basic If/Then in Python Parser of ArcGIS Field Calculator?

                          • Python script for if/elif condition in field calculator







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 5 at 6:27









                          Taras

                          1,8962622




                          1,8962622






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f305018%2fcreating-new-field-in-arcgis-desktop-which-contains-values-from-two-different-co%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

                              Probability when a professor distributes a quiz and homework assignment to a class of n students.

                              Aardman Animations

                              Are they similar matrix