Using python in field calculator to extract highest value from 3 columns
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.
arcmap field-calculator python-parser
New contributor
add a comment |
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.
arcmap field-calculator python-parser
New contributor
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♦
54 mins ago
add a comment |
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.
arcmap field-calculator python-parser
New contributor
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.
arcmap field-calculator python-parser
arcmap field-calculator python-parser
New contributor
New contributor
edited 2 hours ago
PolyGeo♦
53.5k1780239
53.5k1780239
New contributor
asked 3 hours ago
JohnJohn
62
62
New contributor
New contributor
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♦
54 mins ago
add a comment |
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♦
54 mins 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♦
54 mins 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♦
54 mins ago
add a comment |
2 Answers
2
active
oldest
votes
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!])
New contributor
add a comment |
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)
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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!])
New contributor
add a comment |
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!])
New contributor
add a comment |
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!])
New contributor
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!])
New contributor
New contributor
answered 2 hours ago
Tayyab MirTayyab Mir
211
211
New contributor
New contributor
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered 1 hour ago
Aaron♦Aaron
37.9k19109253
37.9k19109253
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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♦
54 mins ago