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












1















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





























    1















    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



























      1












      1








      1








      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 Dec 15 '18 at 3:40









      Vince

      14.5k32748




      14.5k32748










      asked Dec 5 '18 at 6:07









      nishnish

      102




      102






















          4 Answers
          4






          active

          oldest

          votes


















          3














          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































            6














            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 '18 at 6:51








            • 1





              Simplest the best, no pre-logic script, no argument ordering.

              – fatih_dur
              Dec 5 '18 at 8:03



















            3














            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 '18 at 6:43






            • 1





              @BERA, have you ever used any GIS with GUI? =)

              – Taras
              Dec 5 '18 at 7:05






            • 1





              @BERA, P.S. as always a perfect answer

              – Taras
              Dec 5 '18 at 8:14



















            2














            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',
              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%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









              3














              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




























                3














                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


























                  3












                  3








                  3







                  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 '18 at 6:23









                  ahmadhanbahmadhanb

                  22.3k32052




                  22.3k32052

























                      6














                      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 '18 at 6:51








                      • 1





                        Simplest the best, no pre-logic script, no argument ordering.

                        – fatih_dur
                        Dec 5 '18 at 8:03
















                      6














                      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 '18 at 6:51








                      • 1





                        Simplest the best, no pre-logic script, no argument ordering.

                        – fatih_dur
                        Dec 5 '18 at 8:03














                      6












                      6








                      6







                      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 '18 at 6:48









                      FelixIPFelixIP

                      16.1k11642




                      16.1k11642













                      • @nish, to get more information about a shortcut conditional expression, please check this link.

                        – Taras
                        Dec 5 '18 at 6:51








                      • 1





                        Simplest the best, no pre-logic script, no argument ordering.

                        – fatih_dur
                        Dec 5 '18 at 8:03



















                      • @nish, to get more information about a shortcut conditional expression, please check this link.

                        – Taras
                        Dec 5 '18 at 6:51








                      • 1





                        Simplest the best, no pre-logic script, no argument ordering.

                        – fatih_dur
                        Dec 5 '18 at 8:03

















                      @nish, to get more information about a shortcut conditional expression, please check this link.

                      – Taras
                      Dec 5 '18 at 6:51







                      @nish, to get more information about a shortcut conditional expression, please check this link.

                      – Taras
                      Dec 5 '18 at 6:51






                      1




                      1





                      Simplest the best, no pre-logic script, no argument ordering.

                      – fatih_dur
                      Dec 5 '18 at 8:03





                      Simplest the best, no pre-logic script, no argument ordering.

                      – fatih_dur
                      Dec 5 '18 at 8:03











                      3














                      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 '18 at 6:43






                      • 1





                        @BERA, have you ever used any GIS with GUI? =)

                        – Taras
                        Dec 5 '18 at 7:05






                      • 1





                        @BERA, P.S. as always a perfect answer

                        – Taras
                        Dec 5 '18 at 8:14
















                      3














                      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 '18 at 6:43






                      • 1





                        @BERA, have you ever used any GIS with GUI? =)

                        – Taras
                        Dec 5 '18 at 7:05






                      • 1





                        @BERA, P.S. as always a perfect answer

                        – Taras
                        Dec 5 '18 at 8:14














                      3












                      3








                      3







                      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 '18 at 6:45

























                      answered Dec 5 '18 at 6:14









                      BERABERA

                      15.5k52042




                      15.5k52042








                      • 1





                        Thank you. But i wasn't sure where to input this code.

                        – nish
                        Dec 5 '18 at 6:43






                      • 1





                        @BERA, have you ever used any GIS with GUI? =)

                        – Taras
                        Dec 5 '18 at 7:05






                      • 1





                        @BERA, P.S. as always a perfect answer

                        – Taras
                        Dec 5 '18 at 8:14














                      • 1





                        Thank you. But i wasn't sure where to input this code.

                        – nish
                        Dec 5 '18 at 6:43






                      • 1





                        @BERA, have you ever used any GIS with GUI? =)

                        – Taras
                        Dec 5 '18 at 7:05






                      • 1





                        @BERA, P.S. as always a perfect answer

                        – Taras
                        Dec 5 '18 at 8:14








                      1




                      1





                      Thank you. But i wasn't sure where to input this code.

                      – nish
                      Dec 5 '18 at 6:43





                      Thank you. But i wasn't sure where to input this code.

                      – nish
                      Dec 5 '18 at 6:43




                      1




                      1





                      @BERA, have you ever used any GIS with GUI? =)

                      – Taras
                      Dec 5 '18 at 7:05





                      @BERA, have you ever used any GIS with GUI? =)

                      – Taras
                      Dec 5 '18 at 7:05




                      1




                      1





                      @BERA, P.S. as always a perfect answer

                      – Taras
                      Dec 5 '18 at 8:14





                      @BERA, P.S. as always a perfect answer

                      – Taras
                      Dec 5 '18 at 8:14











                      2














                      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




























                        2














                        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


























                          2












                          2








                          2







                          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 '18 at 6:27









                          TarasTaras

                          2,0192624




                          2,0192624






























                              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%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

                              Plaza Victoria

                              Brian Clough

                              Cáceres