<?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: if then statement in field calculator, string to integer in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454172#M35671</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks everyone for your help! Joshua, you were right about calculating on the wrong field. Mathew's method also worked great.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 04 Mar 2014 15:11:41 GMT</pubDate>
    <dc:creator>DavidKoch1</dc:creator>
    <dc:date>2014-03-04T15:11:41Z</dc:date>
    <item>
      <title>if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454164#M35663</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am having a heck of a time trying to write an if elif script in the field calculator. I've read numerous threads but can't figure out how to do it right. I have a field called route_ty_1 that is a string field, with possible attributes of "Bus," "Rail," "LightRail," etc. For a given attribute, I want a code number to be returned for the site_code field, which is short integer. Here is the code I have been tinkering with:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pre-logic script code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def Reclass(site_code):
&amp;nbsp; if route_ty_1 == 'Bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 338
&amp;nbsp; elif route_ty_1 == 'LIGHT RAIL':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 281
&amp;nbsp; elif route_ty_1 == 'METRO':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 335
&amp;nbsp; elif route_ty_1 == 'Rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 282
&amp;nbsp; elif route_ty_1 == 'Commuter Bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 336&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;site_code = &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;Reclass(!site_code!)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If there is a better way to do this than if statements I am very open to suggestions! Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Mar 2014 13:16:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454164#M35663</guid>
      <dc:creator>DavidKoch1</dc:creator>
      <dc:date>2014-03-04T13:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454165#M35664</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am terrible with the field calculator myself, but shouldn't it be (emphasis in bold):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Reclass(&lt;STRONG&gt;route_ty_1&lt;/STRONG&gt;):
&amp;nbsp; if route_ty_1 == 'Bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 338
&amp;nbsp; elif route_ty_1 == 'LIGHT RAIL':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 281
&amp;nbsp; elif route_ty_1 == 'METRO':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 335
&amp;nbsp; elif route_ty_1 == 'Rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 282
&amp;nbsp; elif route_ty_1 == 'Commuter Bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 336&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, maybe have an 'else' in the statement as well to pickup anything that doesn't evaluate in your if statement.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit: upvoted Joshua's solution v:&amp;nbsp; a&amp;nbsp; more complete answer.&amp;nbsp; Mine was lazy &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:15:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454165#M35664</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T20:15:11Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454166#M35665</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello David,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are some interesting options &lt;/SPAN&gt;&lt;A href="http://bytebaker.com/2008/11/03/switch-case-statement-in-python/" rel="nofollow noopener noreferrer" target="_blank"&gt;here&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That being said, I often use the else if method. If I may suggest a few things, I would add a .lower() (makes it case insensitive) and a .strip() (gets rid of any leading or trailing whitespace) to make sure you are matching as many of the cases as possible. I'd also add a else to catch any records that don't match at all.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Reclass(site_code):
&amp;nbsp; site_code=site_code.lower().strip()
&amp;nbsp; if site_code == 'bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 338
&amp;nbsp; elif site_code == 'light rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 281
&amp;nbsp; elif site_code == 'metro':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 335
&amp;nbsp; elif site_code == 'rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 282
&amp;nbsp; elif site_code == 'commuter bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 336
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:15:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454166#M35665</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-11T20:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454167#M35666</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Joshua--should there be anything in the .lower and .strip parentheses?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello David,&lt;BR /&gt;&lt;BR /&gt;There are some interesting options &lt;A href="http://bytebaker.com/2008/11/03/switch-case-statement-in-python/" rel="nofollow noopener noreferrer" target="_blank"&gt;here&lt;/A&gt;.&lt;BR /&gt;&lt;BR /&gt;That being said, I often use the else if method. If I may suggest a few things, I would add a .lower() (makes it case insensitive) and a .strip() (gets rid of any leading or trailing whitespace) to make sure you are matching as many of the cases as possible. I'd also add a else to catch any records that don't match at all.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Reclass(site_code):
&amp;nbsp; site_code=site_code.lower().strip()
&amp;nbsp; if site_code == 'bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 338
&amp;nbsp; elif site_code == 'light rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 281
&amp;nbsp; elif site_code == 'metro':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 335
&amp;nbsp; elif site_code == 'rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 282
&amp;nbsp; elif site_code == 'commuter bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 336
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Good luck!&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:15:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454167#M35666</guid>
      <dc:creator>DavidKoch1</dc:creator>
      <dc:date>2021-12-11T20:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454168#M35667</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks Joshua--should there be anything in the .lower and .strip parentheses?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;Nope!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you want, you can specify characters you want removed from the front and end of a string with the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;strip()&lt;/SPAN&gt;&lt;SPAN&gt; function, but leaving it empty will remove whitespace by default.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Mar 2014 14:25:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454168#M35667</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2014-03-04T14:25:47Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454169#M35668</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks--I am getting the following errors:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;ERROR 000539: Error running expression: Reclass(0) 
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;
&amp;nbsp; File "&amp;lt;string&amp;gt;", line 2, in Reclass
AttributeError: 'int' object has no attribute 'lower'

Failed to execute (CalculateField).
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Nope!&lt;BR /&gt;&lt;BR /&gt;If you want, you can specify characters you want removed from the front and end of a string with the &lt;SPAN style="font-style:italic;"&gt;strip()&lt;/SPAN&gt; function, but leaving it empty will remove whitespace by default.&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:15:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454169#M35668</guid>
      <dc:creator>DavidKoch1</dc:creator>
      <dc:date>2021-12-11T20:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454170#M35669</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks--I am getting the following errors:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;ERROR 000539: Error running expression: Reclass(0) 
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;
&amp;nbsp; File "&amp;lt;string&amp;gt;", line 2, in Reclass
AttributeError: 'int' object has no attribute 'lower'

Failed to execute (CalculateField).
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;I think you're calculating on the wrong field. Based on the strings you're looking for (in the if statements), the input should be a string (not an integer/number).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Make sure the expression reads something like &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;Reclass(!inputTansitType!) &lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It might actually be less confusing if you changed the variable name in the code book:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Reclass(inputTansitType):
&amp;nbsp; inputTansitType=inputTansitType.lower().strip()
&amp;nbsp; if inputTansitType== 'bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 338
&amp;nbsp; elif inputTansitType== 'light rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 281
&amp;nbsp; elif inputTansitType== 'metro':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 335
&amp;nbsp; elif inputTansitType== 'rail':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 282
&amp;nbsp; elif inputTansitType== 'commuter bus':
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 336
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry if I caused any confusion!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:15:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454170#M35669</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-11T20:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454171#M35670</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;As an aside, I find it much easier to use a dictionary in these cases.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;reclass_dict = {'bus': 338,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'light rail': 281,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'metro': 335,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'rail': 282,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'commuter bus': 336,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }

def Reclass(inputTansitType):
&amp;nbsp;&amp;nbsp;&amp;nbsp; inputTansitType = inputTansitType.lower().strip()
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return reclass_dict[inputTansitType]
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:15:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454171#M35670</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T20:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: if then statement in field calculator, string to integer</title>
      <link>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454172#M35671</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks everyone for your help! Joshua, you were right about calculating on the wrong field. Mathew's method also worked great.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Mar 2014 15:11:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-statement-in-field-calculator-string-to/m-p/454172#M35671</guid>
      <dc:creator>DavidKoch1</dc:creator>
      <dc:date>2014-03-04T15:11:41Z</dc:date>
    </item>
  </channel>
</rss>

