<?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: Python code to subtract a value but don't go below 0 in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369169#M77295</link>
    <description>&lt;P&gt;As a one-line function:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def sub_to_zero(a, b): return a - b if a &amp;gt; b else 0&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 11 Jan 2024 20:50:56 GMT</pubDate>
    <dc:creator>DavidSolari</dc:creator>
    <dc:date>2024-01-11T20:50:56Z</dc:date>
    <item>
      <title>Python code to subtract a value but don't go below 0</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369143#M77286</link>
      <description>&lt;P&gt;I need to subtract a value from a field and return that value to the new field, but if the value is below 0 simply return 0. I know I can do this with calculate field but I am unsure of how to write this in Python.&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;&lt;/P&gt;&lt;P&gt;3200-1200=2000&lt;/P&gt;&lt;P&gt;800-1200=0&lt;/P&gt;&lt;P&gt;1211-1200=11&lt;/P&gt;&lt;P&gt;80-1200=0&lt;/P&gt;&lt;P&gt;Thanks for any help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 20:02:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369143#M77286</guid>
      <dc:creator>FlightDeck</dc:creator>
      <dc:date>2024-01-11T20:02:20Z</dc:date>
    </item>
    <item>
      <title>Re: Python code to subtract a value but don't go below 0</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369169#M77295</link>
      <description>&lt;P&gt;As a one-line function:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def sub_to_zero(a, b): return a - b if a &amp;gt; b else 0&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 11 Jan 2024 20:50:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369169#M77295</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2024-01-11T20:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python code to subtract a value but don't go below 0</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369171#M77296</link>
      <description>&lt;P&gt;David just beat me to posting with a very concise option, but here's a multiline option which does the same thing (this part goes in the code block):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def fancy_subtract(subtract_from):
    value = 1200 #define value here
    if subtract_from &amp;lt; value:
        return 0
    else:
        diff = subtract_from - value 
        return diff&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;In a field calculation, you can use this by calling the function on your field:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fancy_subtract(!field_name!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 21:01:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369171#M77296</guid>
      <dc:creator>rwrenner_esri</dc:creator>
      <dc:date>2024-01-11T21:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python code to subtract a value but don't go below 0</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369173#M77297</link>
      <description>&lt;P&gt;There is probably a more elegant or 'pythonic' way to do this but this will do what you want I think:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def calc_sub(num1, num2):
    out = num1-num2
    if out &amp;lt; 0:
        ret_val = 0
    else:
        ret_val = out
    return ret_val
#test using the following:
x = 10
y = 8
print(calc_sub(x,y))#should return 2
print(calc_sub(y,x))#should return 0&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 11 Jan 2024 21:00:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369173#M77297</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2024-01-11T21:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Python code to subtract a value but don't go below 0</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369177#M77298</link>
      <description>&lt;P&gt;can use the max() function.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RhettZufelt_0-1705006943843.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91306i6621E5EE28571156/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RhettZufelt_0-1705006943843.png" alt="RhettZufelt_0-1705006943843.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Takes the max value.&amp;nbsp; Since 0 is larger than any negative number, will return 0&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 21:04:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/python-code-to-subtract-a-value-but-don-t-go-below/m-p/1369177#M77298</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2024-01-11T21:04:32Z</dc:date>
    </item>
  </channel>
</rss>

