<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate a &amp;quot;Race Code&amp;quot; or &amp;quot;Ethnicity Code&amp;quot; field from a &amp;quot;Race&amp;quot; field that is string format in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617700#M74252</link>
    <description>&lt;P&gt;Thank you Ryan for the help. Screenshots are the best!! Even though I didn't select yours as the solution, your suggestion is still an MVP!! ha . Have a good day!&lt;/P&gt;</description>
    <pubDate>Thu, 22 May 2025 18:28:32 GMT</pubDate>
    <dc:creator>eallan</dc:creator>
    <dc:date>2025-05-22T18:28:32Z</dc:date>
    <item>
      <title>Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616803#M74226</link>
      <description>&lt;P&gt;I am trying to convert Race or Ethnicity values into a numeric code for them. For example, "White" would equal "0" , and "American Indian" would equal "1" , and "Latino" would equal "2", and so on and so forth.&lt;/P&gt;&lt;P&gt;I attached a screenshot and inbedded a photo below, that shows my attempt to do this, but I failed miserably. I've been looking online for over 1.5 hours now trying to find an answer or similar example I could go off of but have had no luck.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-05-20 172823.jpg" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/132745i0DEADC40592FBC6D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2025-05-20 172823.jpg" alt="Screenshot 2025-05-20 172823.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 00:55:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616803#M74226</guid>
      <dc:creator>eallan</dc:creator>
      <dc:date>2025-05-21T00:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616810#M74227</link>
      <description>&lt;P&gt;Looks like you've got some basic syntax problems in your code.&lt;/P&gt;&lt;P&gt;To start with, python is indentation-based, and if/elif/else groups should all share the same indentation level.&amp;nbsp; The actions inside those if/elif/else conditions are one indentation level down from there.&amp;nbsp; Also, you do your condition,&amp;nbsp;&lt;EM&gt;then&lt;/EM&gt; your action, like so:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if condition:
    # do stuff
elif other_condition:
    # do other stuff
else:
    # do default stuff&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Second, the field-names-with-exclamation-points thing isn't actually valid python.&amp;nbsp; It's a weird choice ESRI made with the outer layer of the interpreter and is&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;only&lt;/STRONG&gt;&lt;/U&gt; valid in the function call.&amp;nbsp; It will always throw an error if you put it in the code block.&amp;nbsp; This is one of my many gripes with the implementation of Field Calculator.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MErikReedAugusta_0-1747789792023.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/132746i23B9F2B4D0C1901C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MErikReedAugusta_0-1747789792023.png" alt="MErikReedAugusta_0-1747789792023.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lastly, this could be done quite a bit more simply with a list or dictionary lookup.&lt;/P&gt;&lt;P&gt;Try this code in the function call line:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;GetRaceCode(!USER_Race!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then this code in the Code Block:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def GetRaceCode(string_value):
    # I took the order you used, but it honestly looks scrambled.  Make sure
    # to change any numbers that are wrong.  If they're all correct and in
    # order, and the duplicates are separate, then this technically could've
    # been done with a list.  But I'm going with a dictionary for simplicity
    # of editing, for you.
    options = {'White':                                     0,
               'American Indian':                           1,
               'Latino':                                    2,
               'No Response':                               3,
               '____ does not know':                        4,
               '____ asked, but has not provided':          5,
               'Black or African American':                 6,
               'Other':                                     7,
               'Black':                                     8,
               'Asian':                                     9,
               'Hawaiian':                                  10,
               'Native Hawaiian or Other Pacific Islander': 11,
              }

    # First, check to see if it's a valid, and accepted entry.
    # If so, pull it from the list above, and grab the value we said it was.
    # Dictionary calls in Python are formatted as dictionary_name[lookup_key]
    if string_value in options:
        return options[string_value]
    else:
        # NOTE: Zero is also your code for White.  Are you sure you want the
        # failsafe option to match one of the selections?  If not, change
        # this number to something not in the dictionary above.
        return 0 &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 01:24:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616810#M74227</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2025-05-21T01:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616816#M74228</link>
      <description>&lt;P&gt;My screenshot below should work correctly. If you're writing your number values to a string field, then make sure to put quotes around the numbers.&lt;/P&gt;&lt;P&gt;I don't know how to properly explain it, but your Code Block cannot "see" your fields. You can't directly reference them using !Field_Name! syntax. Instead, you have to pass those fields to it from the line above. And then you reference your fields that way. Hopefully my screenshot will help explain it.&lt;/P&gt;&lt;P&gt;From there, you just have your if statements to calculate your values. And then at the very end, you need to return the value of "Race_Code" so it can calculate value in your table.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RyanUthoff_1-1747791731422.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/132748i8380D9ED5B25387E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RyanUthoff_1-1747791731422.png" alt="RyanUthoff_1-1747791731422.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/569244"&gt;@MErikReedAugusta&lt;/a&gt;&amp;nbsp;replied as I was typing my response and didn't see it until after I posted. Their method is more elegant and better than mine lol.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 01:47:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616816#M74228</guid>
      <dc:creator>RyanUthoff</dc:creator>
      <dc:date>2025-05-21T01:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616818#M74229</link>
      <description>&lt;P&gt;Instead of:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    if string_value in options:
        return options[string_value]
    else:
        return 0 &lt;/LI-CODE&gt;&lt;P&gt;You can use:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;options.get(string_value, 0)  #  or replace 0 with preferred default value&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 21 May 2025 02:56:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616818#M74229</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2025-05-21T02:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616996#M74230</link>
      <description>&lt;P&gt;Clever!&amp;nbsp; Somehow I've never encountered/used this approach, and I love it.&amp;nbsp; Definitely saving for the future.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 14:40:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1616996#M74230</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2025-05-21T14:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617103#M74233</link>
      <description>&lt;P&gt;If you don't actually care about the specific codes assigned to the values in that column, and just want to map specific string values to an integer, you can use a generic solution like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;CODES: list[str] = []

def GetCode(value: str) -&amp;gt; int:
    # Pass Null (You can replace None with a default value like -1)
    if not value:
        return None
    
    if value not in CODES:
        CODES.append(value)
    return CODES.index(value)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This just creates a running list of all the string values it finds and uses the list index of the string as the return value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This way you don't need to create a mapping of all possible values and can just let Python do it for you.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 16:20:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617103#M74233</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-05-21T16:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617106#M74234</link>
      <description>&lt;P&gt;There is a tutorial that includes a simple example of how to conditionally calculate values in one field based on values in another:&lt;/P&gt;&lt;P&gt;&lt;A href="https://learn.arcgis.com/en/projects/identify-schools-for-a-mentoring-program/#subsection-18:~:text=add%20values%20in%20this%20field%20to%20make%20it%20easier" target="_blank"&gt;https://learn.arcgis.com/en/projects/identify-schools-for-a-mentoring-program/#subsection-18:~:text=add%20values%20in%20this%20field%20to%20make%20it%20easier&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 16:25:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617106#M74234</guid>
      <dc:creator>BobBooth1</dc:creator>
      <dc:date>2025-05-21T16:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617363#M74240</link>
      <description>&lt;P&gt;Holy moly,&lt;/P&gt;&lt;P&gt;I totally missed all these amazing responses this morning. Apologies for not getting back to yall sooner and THANK YOU ALL SO MUCH for the help!!! I have to end my workday soon so will get back to this tomorrow morning.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 00:28:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617363#M74240</guid>
      <dc:creator>eallan</dc:creator>
      <dc:date>2025-05-22T00:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617700#M74252</link>
      <description>&lt;P&gt;Thank you Ryan for the help. Screenshots are the best!! Even though I didn't select yours as the solution, your suggestion is still an MVP!! ha . Have a good day!&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 18:28:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617700#M74252</guid>
      <dc:creator>eallan</dc:creator>
      <dc:date>2025-05-22T18:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617701#M74253</link>
      <description>&lt;P&gt;Thank you for that link Bob, I saved it in my notes and bookmarked it.&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 18:32:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617701#M74253</guid>
      <dc:creator>eallan</dc:creator>
      <dc:date>2025-05-22T18:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a "Race Code" or "Ethnicity Code" field from a "Race" field that is string format</title>
      <link>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617702#M74254</link>
      <description>&lt;P&gt;Thank you Hayden for sharing that, I put it in my notes and bookmarked it. MUCH APPRECIATED!!!&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 18:32:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-a-quot-race-code-quot-or-quot-ethnicity/m-p/1617702#M74254</guid>
      <dc:creator>eallan</dc:creator>
      <dc:date>2025-05-22T18:32:49Z</dc:date>
    </item>
  </channel>
</rss>

