<?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 How can you calculate Q1,Q2,Q3, and IQR using ESRI? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360228#M28413</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I hope the answer does not require Geostatistical Analyst!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To calculate these statistical values you have to first sort the values by ascending order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Q2 is the mean or the total number of values divided by two and it gives you the position of the value in terms of n. so if you had 100 values then median is the (value at position 50) plus the (value at position 51) divided by 2. If you had 101 values the median would be the value at position 51.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have several hundred shape files with values that I want to apply this calculation to.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to use a combination of python and model builder to calculate these, calculate and upper and lower "fence" and then find the values that are above and below these "fences".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I had the work flow all worked out! until I tried to create a new sequential number field based on the ascending sort.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I found this code:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def autoIncrement(start=0,step=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; i=start
&amp;nbsp;&amp;nbsp;&amp;nbsp; while 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield i
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i+=step
incrementCursor = arcpy.UpdateCursor(table_name) #There is no guarantee of order here
incrementer = autoIncrement(10,2)
for row in incrementCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(field, incrementer.next()) #Note use of next method
&amp;nbsp;&amp;nbsp;&amp;nbsp; incrementCursor.updateRow(row)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I changed table_name to "Name of shape file" with target field&lt;/P&gt;&lt;P&gt;I changed field to 'name of the field' in said shape file&lt;/P&gt;&lt;P&gt;selected the column in the open attribute table&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I pressed enter at the end of (row) it just went down one line..I pressed it again&lt;/P&gt;&lt;P&gt;and it went back to &amp;gt;&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;none of the rows were updated. I thought the cursor is what you have selected in your map document.&lt;/P&gt;&lt;P&gt;so I need to&lt;/P&gt;&lt;P&gt;1. Tell each shape file to sort the target values in ascending order (used sort tool with iterater in model builder)&lt;/P&gt;&lt;P&gt;2. create a script that populates a new field with sequential numbers based on the new sort.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help on this is much appreciated!&lt;/P&gt;&lt;P&gt;Dan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 16:48:18 GMT</pubDate>
    <dc:creator>DanielAmrine</dc:creator>
    <dc:date>2021-12-11T16:48:18Z</dc:date>
    <item>
      <title>How can you calculate Q1,Q2,Q3, and IQR using ESRI?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360228#M28413</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I hope the answer does not require Geostatistical Analyst!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To calculate these statistical values you have to first sort the values by ascending order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Q2 is the mean or the total number of values divided by two and it gives you the position of the value in terms of n. so if you had 100 values then median is the (value at position 50) plus the (value at position 51) divided by 2. If you had 101 values the median would be the value at position 51.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have several hundred shape files with values that I want to apply this calculation to.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to use a combination of python and model builder to calculate these, calculate and upper and lower "fence" and then find the values that are above and below these "fences".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I had the work flow all worked out! until I tried to create a new sequential number field based on the ascending sort.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I found this code:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def autoIncrement(start=0,step=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; i=start
&amp;nbsp;&amp;nbsp;&amp;nbsp; while 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield i
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i+=step
incrementCursor = arcpy.UpdateCursor(table_name) #There is no guarantee of order here
incrementer = autoIncrement(10,2)
for row in incrementCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(field, incrementer.next()) #Note use of next method
&amp;nbsp;&amp;nbsp;&amp;nbsp; incrementCursor.updateRow(row)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I changed table_name to "Name of shape file" with target field&lt;/P&gt;&lt;P&gt;I changed field to 'name of the field' in said shape file&lt;/P&gt;&lt;P&gt;selected the column in the open attribute table&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I pressed enter at the end of (row) it just went down one line..I pressed it again&lt;/P&gt;&lt;P&gt;and it went back to &amp;gt;&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;none of the rows were updated. I thought the cursor is what you have selected in your map document.&lt;/P&gt;&lt;P&gt;so I need to&lt;/P&gt;&lt;P&gt;1. Tell each shape file to sort the target values in ascending order (used sort tool with iterater in model builder)&lt;/P&gt;&lt;P&gt;2. create a script that populates a new field with sequential numbers based on the new sort.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help on this is much appreciated!&lt;/P&gt;&lt;P&gt;Dan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:48:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360228#M28413</guid>
      <dc:creator>DanielAmrine</dc:creator>
      <dc:date>2021-12-11T16:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: How can you calculate Q1,Q2,Q3, and IQR using ESRI?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360229#M28414</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/sort.htm" title="http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/sort.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Sort—Help | ArcGIS for Desktop&lt;/A&gt; with an advanced license, produces a new output file which I am sure you don't want.&lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/latest/tools/analysis-toolbox/summary-statistics.htm" title="http://desktop.arcgis.com/en/arcmap/latest/tools/analysis-toolbox/summary-statistics.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Summary Statistics—Help | ArcGIS for Desktop&lt;/A&gt;&amp;nbsp; doesn't include the median let alone the Q's&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.arcgis.com/home/item.html?id=6c384f06c9f14d09920f4ff14460f4e2" rel="nofollow noopener noreferrer" target="_blank"&gt;Field Statistics&amp;nbsp; &lt;/A&gt;​has some code you can steal to at least get the median and the sorting, but it doesn't produce a field&lt;/P&gt;&lt;P&gt;There are other means, with builtin tools... but it would then require you to do&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;read the column&lt;/LI&gt;&lt;LI&gt;calculate the values&lt;/LI&gt;&lt;LI&gt;reclassify the initial column into the Q's&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; p_75 = np.percentile(a,75.0,axis=0)
&amp;gt;&amp;gt;&amp;gt; p_50 = np.percentile(a,50.0,axis=0)
&amp;gt;&amp;gt;&amp;gt; p_25 = np.percentile(a,25.0,axis=0)
&amp;gt;&amp;gt;&amp;gt; a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
&amp;gt;&amp;gt;&amp;gt; p_25
3.25
&amp;gt;&amp;gt;&amp;gt; p50
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;string&amp;gt;", line 1, in &amp;lt;module&amp;gt;
NameError: name 'p50' is not defined
&amp;gt;&amp;gt;&amp;gt; p_50
5.5
&amp;gt;&amp;gt;&amp;gt; p_75
7.75&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have some reclassification code, or I am sure you can emulate it.&lt;/P&gt;&lt;P&gt;So the big problem is, you need to read the column via searchcursor use numpy (which you have ) to determine Q25, Q50 (median) and Q75, then... reclass the initial values into a new column&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:48:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360229#M28414</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T16:48:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can you calculate Q1,Q2,Q3, and IQR using ESRI?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360230#M28415</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can use &lt;A href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.percentileofscore.html" rel="nofollow noopener noreferrer" target="_blank"&gt;scipy.stats.percentileofscore()&lt;/A&gt; for this (not sure if I installed it separate or if it came with ArcGIS).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; import numpy, scipy.stats
... fc = 'points'
... field = 'val'
... rankfield = 'rank'
... values = [row[0] for row in arcpy.da.SearchCursor(fc, field)] # load values to list
... with arcpy.da.UpdateCursor(fc, [field,rankfield]) as cursor:
...&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = scipy.stats.percentileofscore(values,row[0]) # returns percentile rank at value
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:48:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360230#M28415</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T16:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: How can you calculate Q1,Q2,Q3, and IQR using ESRI?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360231#M28416</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dan Patterson,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is very helpful in knowing there are tools to calculate these! I guess the next step is how do you take the values from a column in the attribute table and write them to the format you have shown [n, n+1, etc...] is this an array?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I did find some code that wrote the values of two fields in a dictionary and then you could do a "vlookup" kind of deal (drawing from EXCEL) to insert the value of one field and have it print the value of the "lookup field".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This still doesn't solve how to add an numbered "ID" field based on the ascending sort of the values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example I could use model builder to get the row count of each shape file...and then use that to calculate the Q1, Q2, and Q3...and then with the dictionaries I could use python to use the calculated values to pull the appropriate value from the target field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;in a shape file with 380 records the Q2 or P_50 would equal (n+1)*0.5 or 190.5 so I would have to pull the target value at 190 and add it to the target value at 191 and divide by 2. This would give me the median target value. You round down and take the next value when n is even.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That's essentially the workflow and I could do it in Excel pretty quickly but I'd have to do it for each individual shape file after exporting each individual table!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so it all boils down to having the target value field and then another field with a sequential number field based on the ascending sort.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately ESRI made everything dependent on the "FID" field so all of their autoInctrements are based on this field regardless of how the table is sorted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Dan Amrine&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Mar 2016 21:43:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360231#M28416</guid>
      <dc:creator>DanielAmrine</dc:creator>
      <dc:date>2016-03-18T21:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: How can you calculate Q1,Q2,Q3, and IQR using ESRI?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360232#M28417</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;the trick is to read the column of data, like &lt;A href="https://community.esri.com/migrated-users/19932"&gt;Darren Wiens&lt;/A&gt;​ pointed out in his searchcursor approach (you can use plain NumPy is you are using python 2.7 or SciPy if you have python 3.4 installed) .&amp;nbsp; &lt;/P&gt;&lt;P&gt;In pure python/nNumpy/SciPy there is&amp;nbsp; FeatureClassToNumPy or TableToNumPy array method ... just specifying the FC or tbl that you are using, the field or fields that you want.&amp;nbsp; It is read into memory and you process it in numpy or scipy (scipy isn't needed for most things).... then you do one of two things.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;using Darren's approach, you would have to read the field in, do the processing, then use a search cursor to write the results out.&lt;/LI&gt;&lt;LI&gt;using the numpy approach, you use NumPyArrayToTable to make a table of the results which you join back to the original FC or table using the OBJECTID or FID field as the key field.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Comparison&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;UL&gt;&lt;LI&gt;option 1 is slower since you are reading the table twice... no biggy perhaps&lt;/LI&gt;&lt;LI&gt;option 2, the results are joined, hence not permanent, but .... of course.... arcpy has a method to join a numpy table results to a FC or shapefile.... alternately you just make the join&amp;nbsp; permanent, save to a new FC or table&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;My preference is to use the join approach.&amp;nbsp; If I want to skip the join and make a permanent FC or table, I just read the whole FC/table in, specify the field for processing, do the work, then send it back out using NumPyArrayToFeatureclass or NumPyArrayToTable..&lt;/P&gt;&lt;P&gt;I haven't used a searchcursor, updatecursor or insertcursor in quite a while.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So the choice is yours, sorting data on the OIDNAME or FID name is no sweat in numpy (or SciPy), so all your work can be unravelled back to the original order.&amp;nbsp; I use this approach regularly to produce shapefiles sorted by geometric properties for some of the work I do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The tools are there, esri has silently without fanfare given access to do a lot of things in vector and raster world that require a higher license level simply because you can do the work in numpy or SciPy and bring it back into ArcMap.&amp;nbsp; The nice thing is, once you have an array, you can work in a variety of other environments or languages which offer different capabilities.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So the choice is yours... you can use a maximized combination of arcpy and numpy (ie via cursors) or a minimized combination of arcpy/numpy with most of the emphasis on numpy&lt;/P&gt;&lt;P&gt;Good luck... have a look at Darren's approach should you have immediate needs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PS&lt;/P&gt;&lt;P&gt; &lt;A href="https://community.esri.com/group/1732"&gt;NumPy Repository&lt;/A&gt;​ is a place I house musings and works related to array work.&amp;nbsp; If you have a background already in that kind of work, it may be of interest to you at some stage (it assumes a certain level of python familiarity and works presented require python 3.4+... which you can get by installing ArcGIS PRO or by using an Anaconda distribution which includes the whole stack)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Mar 2016 22:10:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-you-calculate-q1-q2-q3-and-iqr-using-esri/m-p/360232#M28417</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-03-18T22:10:42Z</dc:date>
    </item>
  </channel>
</rss>

