<?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: Excel formula with lookup tables for field calculation in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15809#M1223</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;there are a number of ways to do this probably.&amp;nbsp; I think the easiest would be using an Update Cursor to cycle through your feature class and implement a series of if/elif/else statements to update the values in your table.&amp;nbsp; that logic might look something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.UpdateCursor(FeatureClass,'','','updateField')
for row in rows:
 if row.updateField == 1:
&amp;nbsp; row.updateField = *some calculation*
&amp;nbsp; rows.updateRow(row)
 elif row.updateField == 2:
&amp;nbsp; row.updateField = *some calculation*
&amp;nbsp; rows.updateRow(row)
 elif ...another condition to test for..&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are otherways you could implement this, but depending on the number of conditions you have to test for this should be pretty easy.&amp;nbsp; Situations where you have several values that get updated by the same formula could use an "in" operator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if row.updateField in (1,5,10,11) :&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The other trick to this is you have to be able to translate your excel formula into a calculation that can be performed in python. For simple calcs this shouldn't be hard but if the formulas contain references to other cells or worksheets then that becomes increasingly difficult though not impossible.&amp;nbsp; In this situation as the other respondant suggested you might simply make those calculations in excel, save the results (values, not formulas and formats!) as a .csv and then join that back to your GIS data and calculate the the fields from the joined table.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 20:38:02 GMT</pubDate>
    <dc:creator>ChristopherThompson</dc:creator>
    <dc:date>2021-12-10T20:38:02Z</dc:date>
    <item>
      <title>Excel formula with lookup tables for field calculation</title>
      <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15807#M1221</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The challenge I have is to &lt;/SPAN&gt;&lt;STRONG&gt;calculate a new value for one of the field columns of the attribute table&lt;/STRONG&gt;&lt;SPAN&gt; of a normal polygon layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration:underline;"&gt;Precondition&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;STRONG&gt;use a formula from an Excel spredsheet which works utilizing lookup tables&lt;/STRONG&gt;&lt;SPAN&gt; (data for the formula).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas for the easiest approach? Maybe do all the calculations outside ARcGIS in a database and then feed the field with the new value?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sergio&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 09:19:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15807#M1221</guid>
      <dc:creator>SergioMoreno_Rojas</dc:creator>
      <dc:date>2012-07-26T09:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: Excel formula with lookup tables for field calculation</title>
      <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15808#M1222</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You could instantiate your excel logic in python using python dictionaries as the look-up lists...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or pre-calculate everything in excel and join it to your spatial layers.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 13:50:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15808#M1222</guid>
      <dc:creator>AustinDavis</dc:creator>
      <dc:date>2012-07-26T13:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: Excel formula with lookup tables for field calculation</title>
      <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15809#M1223</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;there are a number of ways to do this probably.&amp;nbsp; I think the easiest would be using an Update Cursor to cycle through your feature class and implement a series of if/elif/else statements to update the values in your table.&amp;nbsp; that logic might look something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.UpdateCursor(FeatureClass,'','','updateField')
for row in rows:
 if row.updateField == 1:
&amp;nbsp; row.updateField = *some calculation*
&amp;nbsp; rows.updateRow(row)
 elif row.updateField == 2:
&amp;nbsp; row.updateField = *some calculation*
&amp;nbsp; rows.updateRow(row)
 elif ...another condition to test for..&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are otherways you could implement this, but depending on the number of conditions you have to test for this should be pretty easy.&amp;nbsp; Situations where you have several values that get updated by the same formula could use an "in" operator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if row.updateField in (1,5,10,11) :&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The other trick to this is you have to be able to translate your excel formula into a calculation that can be performed in python. For simple calcs this shouldn't be hard but if the formulas contain references to other cells or worksheets then that becomes increasingly difficult though not impossible.&amp;nbsp; In this situation as the other respondant suggested you might simply make those calculations in excel, save the results (values, not formulas and formats!) as a .csv and then join that back to your GIS data and calculate the the fields from the joined table.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:38:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15809#M1223</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-10T20:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: Excel formula with lookup tables for field calculation</title>
      <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15810#M1224</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Another level of difficulty on this is that all the process has to be automatic, as it will be a &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;processing service&lt;/SPAN&gt;&lt;SPAN&gt; for Flex Viewer in a company webportal based on user defined parameters for the Excel formula.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Not sure if pre-calculate everything in Excel and join it to the spatial layers can be done automatic: &lt;/SPAN&gt;&lt;STRONG&gt;ArcGIS&lt;/STRONG&gt;&lt;SPAN&gt; (export table values) -&amp;gt; &lt;/SPAN&gt;&lt;STRONG&gt;Excel&lt;/STRONG&gt;&lt;SPAN&gt; (calc) -&amp;gt; &lt;/SPAN&gt;&lt;STRONG&gt;ArcGIS &lt;/STRONG&gt;&lt;SPAN&gt;(feed table with the new values)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know ArcGIS is not a software designed for complex calculations like Excel but maybe writing it in Python could solve it? Do this within a model to allow publishing the service afterwards?&amp;nbsp; FEASIBLE????&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I will investigate about writing the excel logic in python using python dictionaries as the look-up lists...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks everybody&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Jul 2012 10:19:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15810#M1224</guid>
      <dc:creator>SergioMoreno_Rojas</dc:creator>
      <dc:date>2012-07-27T10:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Excel formula with lookup tables for field calculation</title>
      <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15811#M1225</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;can you provide an example of a formula that is being used and the look up tables?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Jul 2012 10:28:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15811#M1225</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-07-27T10:28:42Z</dc:date>
    </item>
    <item>
      <title>Re: Excel formula with lookup tables for field calculation</title>
      <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15812#M1226</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;can you provide an example of a formula that is being used and the look up tables?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Check out the formula in the 'P2O5 (kg/ha)' coloured box within the Excel example spredsheet attached.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Jul 2012 11:13:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15812#M1226</guid>
      <dc:creator>SergioMoreno_Rojas</dc:creator>
      <dc:date>2012-07-27T11:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: Excel formula with lookup tables for field calculation</title>
      <link>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15813#M1227</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Wow, that is possibly the longest excel formula i have ever seen.. I nearly fell out of my chair! i have 2 monitors and could not stretch out excel wide enough to view the whole thing.. ! I am truly impressed. LOL&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That said, without dissecting the whole thing I mostly see how it works and i think you can do this with using dictionaries that have multiple values per key.. .so just the first row of your removal data would look like this in a dictionary:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;removal_data = {'Field Beans':[11.0,11.0,12.0,12.0]}&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and so this part of your excel formula:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;'$J$29*(VLOOKUP($J$28,RemovalData,3,0))'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;could be implemented by getting a value from a dictionary based on the key reference. To do this, first make a function to get a value from the dictionary above, the arguments are the dictionary name, the key you're interested in and the index of the column holding the data you want. Remember in excel the column indexes are 1 based but in python indexes are 0 based, so your column references will always be 1 less than in excel&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def get_val(dname,key,col_idx):
 d_cols = dname[key]
 return d_cols[col_idx]&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;In this example value for $J$29 is variable J29, and variable for $J$28 is J28 - then in the formula this would be implmented by:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;J29 = 10 #some number provided by user input or derived from some other process in your code
J28 = 'Field Beans' #again, how this is supplied is a matter of how the overall program will work - user input or from some other process inside the program, this could be based on a list of the keys in a given dictionary for instance

result = J29 * (get_val(removal_data,J28,2))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Ultimately it looks like you need to handle the following inputs to the formula:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;a variable to contain the data Column B (B2 for instance)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A dictionary with multiple key values for Removal Data&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A dictionary with multiple key values for Manure Data&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;a variable to contain the value for PTarget&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Beyond that, there are multiple nested If/elif/else type statements and some basic mathmatical operations. Complex mostly in the nesting of the If/elif/else and the use of the lookups, but very doable I think.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 15:54:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/excel-formula-with-lookup-tables-for-field/m-p/15813#M1227</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-12T15:54:55Z</dc:date>
    </item>
  </channel>
</rss>

