Using python in field calculator to extract highest value from 3 columns












2















I have three columns, one for percentage White, percentage Hispanic, and percentage African American per census tract. I'm trying to use python to extract the population subtype with the highest percentage into a new column called "highest_percentage". The highest percentage category will have 3 classes: 1,2, and 3. I can not figure out how to set a conditional statement so that I can automatically assign a value of 1,2, or 3 to the census tract to use in a classified map. Any help would be appreciated! I was able to write code to find the highest percentage and assign the new field the same value as that percentage, but i'm trying to find which field has the highest value and assign it 1-3 based on a classification. 3 white, 2 Hispanic, 1 African American



I've attached a screenshot of the table and the code window below.



screenshot of arcmap attribute table



Arcmap python field calculator



second arcmpa attribute table sceenshot










share|improve this question









New contributor




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





















  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please always present any code using formatted text (highlight it use the {} button) rather than pictures.

    – PolyGeo
    2 hours ago
















2















I have three columns, one for percentage White, percentage Hispanic, and percentage African American per census tract. I'm trying to use python to extract the population subtype with the highest percentage into a new column called "highest_percentage". The highest percentage category will have 3 classes: 1,2, and 3. I can not figure out how to set a conditional statement so that I can automatically assign a value of 1,2, or 3 to the census tract to use in a classified map. Any help would be appreciated! I was able to write code to find the highest percentage and assign the new field the same value as that percentage, but i'm trying to find which field has the highest value and assign it 1-3 based on a classification. 3 white, 2 Hispanic, 1 African American



I've attached a screenshot of the table and the code window below.



screenshot of arcmap attribute table



Arcmap python field calculator



second arcmpa attribute table sceenshot










share|improve this question









New contributor




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





















  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please always present any code using formatted text (highlight it use the {} button) rather than pictures.

    – PolyGeo
    2 hours ago














2












2








2








I have three columns, one for percentage White, percentage Hispanic, and percentage African American per census tract. I'm trying to use python to extract the population subtype with the highest percentage into a new column called "highest_percentage". The highest percentage category will have 3 classes: 1,2, and 3. I can not figure out how to set a conditional statement so that I can automatically assign a value of 1,2, or 3 to the census tract to use in a classified map. Any help would be appreciated! I was able to write code to find the highest percentage and assign the new field the same value as that percentage, but i'm trying to find which field has the highest value and assign it 1-3 based on a classification. 3 white, 2 Hispanic, 1 African American



I've attached a screenshot of the table and the code window below.



screenshot of arcmap attribute table



Arcmap python field calculator



second arcmpa attribute table sceenshot










share|improve this question









New contributor




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












I have three columns, one for percentage White, percentage Hispanic, and percentage African American per census tract. I'm trying to use python to extract the population subtype with the highest percentage into a new column called "highest_percentage". The highest percentage category will have 3 classes: 1,2, and 3. I can not figure out how to set a conditional statement so that I can automatically assign a value of 1,2, or 3 to the census tract to use in a classified map. Any help would be appreciated! I was able to write code to find the highest percentage and assign the new field the same value as that percentage, but i'm trying to find which field has the highest value and assign it 1-3 based on a classification. 3 white, 2 Hispanic, 1 African American



I've attached a screenshot of the table and the code window below.



screenshot of arcmap attribute table



Arcmap python field calculator



second arcmpa attribute table sceenshot







arcmap field-calculator python-parser






share|improve this question









New contributor




John 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 question









New contributor




John 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 question




share|improve this question








edited 3 hours ago









PolyGeo

53.5k1780239




53.5k1780239






New contributor




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









asked 4 hours ago









JohnJohn

112




112




New contributor




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





New contributor





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






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













  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please always present any code using formatted text (highlight it use the {} button) rather than pictures.

    – PolyGeo
    2 hours ago



















  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please always present any code using formatted text (highlight it use the {} button) rather than pictures.

    – PolyGeo
    2 hours ago

















Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please always present any code using formatted text (highlight it use the {} button) rather than pictures.

– PolyGeo
2 hours ago





Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please always present any code using formatted text (highlight it use the {} button) rather than pictures.

– PolyGeo
2 hours ago










2 Answers
2






active

oldest

votes


















3














Try this



Pre-Logic Script Code:



def maxnum(fields):
max_value=fields[0]
return_value=1
if (fields[1]>max_value):
max_value=fields[1]
return_value=2
if (fields[2]>max_value):
max_value=fields[2]
return_value=3
return return_value


trav_tracts.highest_percent=



maxnum([!trac_tracts.percent_aa!,!trav_tracts.percent_hispanic!,!trac_tracts.percent_white!])





share|improve this answer








New contributor




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




























    2














    I prefer to use an arcpy UpdateCursor for tasks such as these, mainly because I think the syntax is easier to work with and can be incorporated into larger workflows.



    In your case, find the index of the largest percentage in each row. For example:



    import arcpy

    fc = r'C:pathtoyourfile_geodatabase.gdbfeatureclass'

    with arcpy.da.UpdateCursor(fc, ("african_american", "hispanic", "white", "index")) as cursor:
    for row in cursor:
    max_val = max(row) # Find the max value in each row
    max_index = row.index(max_val) # Find the index of the max value
    row[3] = max_index + 1 # Apply a correction on the index
    cursor.updateRow(row)




    enter image description here






    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
      });


      }
      });






      John is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f312155%2fusing-python-in-field-calculator-to-extract-highest-value-from-3-columns%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Try this



      Pre-Logic Script Code:



      def maxnum(fields):
      max_value=fields[0]
      return_value=1
      if (fields[1]>max_value):
      max_value=fields[1]
      return_value=2
      if (fields[2]>max_value):
      max_value=fields[2]
      return_value=3
      return return_value


      trav_tracts.highest_percent=



      maxnum([!trac_tracts.percent_aa!,!trav_tracts.percent_hispanic!,!trac_tracts.percent_white!])





      share|improve this answer








      New contributor




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

























        3














        Try this



        Pre-Logic Script Code:



        def maxnum(fields):
        max_value=fields[0]
        return_value=1
        if (fields[1]>max_value):
        max_value=fields[1]
        return_value=2
        if (fields[2]>max_value):
        max_value=fields[2]
        return_value=3
        return return_value


        trav_tracts.highest_percent=



        maxnum([!trac_tracts.percent_aa!,!trav_tracts.percent_hispanic!,!trac_tracts.percent_white!])





        share|improve this answer








        New contributor




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























          3












          3








          3







          Try this



          Pre-Logic Script Code:



          def maxnum(fields):
          max_value=fields[0]
          return_value=1
          if (fields[1]>max_value):
          max_value=fields[1]
          return_value=2
          if (fields[2]>max_value):
          max_value=fields[2]
          return_value=3
          return return_value


          trav_tracts.highest_percent=



          maxnum([!trac_tracts.percent_aa!,!trav_tracts.percent_hispanic!,!trac_tracts.percent_white!])





          share|improve this answer








          New contributor




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










          Try this



          Pre-Logic Script Code:



          def maxnum(fields):
          max_value=fields[0]
          return_value=1
          if (fields[1]>max_value):
          max_value=fields[1]
          return_value=2
          if (fields[2]>max_value):
          max_value=fields[2]
          return_value=3
          return return_value


          trav_tracts.highest_percent=



          maxnum([!trac_tracts.percent_aa!,!trav_tracts.percent_hispanic!,!trac_tracts.percent_white!])






          share|improve this answer








          New contributor




          Tayyab Mir 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






          New contributor




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









          answered 3 hours ago









          Tayyab MirTayyab Mir

          311




          311




          New contributor




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





          New contributor





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






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

























              2














              I prefer to use an arcpy UpdateCursor for tasks such as these, mainly because I think the syntax is easier to work with and can be incorporated into larger workflows.



              In your case, find the index of the largest percentage in each row. For example:



              import arcpy

              fc = r'C:pathtoyourfile_geodatabase.gdbfeatureclass'

              with arcpy.da.UpdateCursor(fc, ("african_american", "hispanic", "white", "index")) as cursor:
              for row in cursor:
              max_val = max(row) # Find the max value in each row
              max_index = row.index(max_val) # Find the index of the max value
              row[3] = max_index + 1 # Apply a correction on the index
              cursor.updateRow(row)




              enter image description here






              share|improve this answer




























                2














                I prefer to use an arcpy UpdateCursor for tasks such as these, mainly because I think the syntax is easier to work with and can be incorporated into larger workflows.



                In your case, find the index of the largest percentage in each row. For example:



                import arcpy

                fc = r'C:pathtoyourfile_geodatabase.gdbfeatureclass'

                with arcpy.da.UpdateCursor(fc, ("african_american", "hispanic", "white", "index")) as cursor:
                for row in cursor:
                max_val = max(row) # Find the max value in each row
                max_index = row.index(max_val) # Find the index of the max value
                row[3] = max_index + 1 # Apply a correction on the index
                cursor.updateRow(row)




                enter image description here






                share|improve this answer


























                  2












                  2








                  2







                  I prefer to use an arcpy UpdateCursor for tasks such as these, mainly because I think the syntax is easier to work with and can be incorporated into larger workflows.



                  In your case, find the index of the largest percentage in each row. For example:



                  import arcpy

                  fc = r'C:pathtoyourfile_geodatabase.gdbfeatureclass'

                  with arcpy.da.UpdateCursor(fc, ("african_american", "hispanic", "white", "index")) as cursor:
                  for row in cursor:
                  max_val = max(row) # Find the max value in each row
                  max_index = row.index(max_val) # Find the index of the max value
                  row[3] = max_index + 1 # Apply a correction on the index
                  cursor.updateRow(row)




                  enter image description here






                  share|improve this answer













                  I prefer to use an arcpy UpdateCursor for tasks such as these, mainly because I think the syntax is easier to work with and can be incorporated into larger workflows.



                  In your case, find the index of the largest percentage in each row. For example:



                  import arcpy

                  fc = r'C:pathtoyourfile_geodatabase.gdbfeatureclass'

                  with arcpy.da.UpdateCursor(fc, ("african_american", "hispanic", "white", "index")) as cursor:
                  for row in cursor:
                  max_val = max(row) # Find the max value in each row
                  max_index = row.index(max_val) # Find the index of the max value
                  row[3] = max_index + 1 # Apply a correction on the index
                  cursor.updateRow(row)




                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  AaronAaron

                  37.9k19109253




                  37.9k19109253






















                      John is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      John is a new contributor. Be nice, and check out our Code of Conduct.













                      John is a new contributor. Be nice, and check out our Code of Conduct.












                      John is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f312155%2fusing-python-in-field-calculator-to-extract-highest-value-from-3-columns%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

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)