<?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: Pivot: Zonal Histogram Results Table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420760#M33056</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Truly appreciate your ongoing help and advice. &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 12 Jul 2016 19:45:23 GMT</pubDate>
    <dc:creator>PeterWilson</dc:creator>
    <dc:date>2016-07-12T19:45:23Z</dc:date>
    <item>
      <title>Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420754#M33050</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm busy summarizing the landuse found within each of my watersheds using &lt;A href="http://desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/zonal-histogram.htm"&gt;Zonal Histogram&lt;/A&gt;. I'd like to pivot the Hydro (i.e. Hydro_1236 etc.) columns to rows and pivot the Label Column values to column headings. I've attached the Zonal Histogram Results Table as well as a mock-up of the table structure that I'm looking for. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Watersheds_Landuse_160607.PNG" class="image-6 jive-image" src="https://community.esri.com/legacyfs/online/210839_Watersheds_Landuse_160607.PNG" style="width: 620px; height: 709px;" /&gt;&lt;/P&gt;&lt;P&gt;Watersheds + Land-use Raster&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Zonal_Histogram_Results_160607.PNG" class="image-2 jive-image" height="113" src="https://community.esri.com/legacyfs/online/210834_Zonal_Histogram_Results_160607.PNG" style="width: 827px; height: 123.286px;" width="758" /&gt;&lt;/P&gt;&lt;P&gt;Zonal Histogram Results Table&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Landuse_Classification_160607.PNG" class="image-3 jive-image" src="https://community.esri.com/legacyfs/online/210835_Landuse_Classification_160607.PNG" style="width: 620px; height: 468px;" /&gt;&lt;/P&gt;&lt;P&gt;Landuse Raster: Classification&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Landuse_Labels_160607.PNG" class="image-4 jive-image" src="https://community.esri.com/legacyfs/online/210836_Landuse_Labels_160607.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;Landuse Raster: Labels&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Pivot_Results_Table_160706.PNG" class="image-5 jive-image" height="452" src="https://community.esri.com/legacyfs/online/210838_Pivot_Results_Table_160706.PNG" style="width: 824px; height: 491.742px;" width="758" /&gt;&lt;/P&gt;&lt;P&gt;Pivot Results Table:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that the HydroID field values were based on the following fields: Hydro_1236 etc. I removed "Hydro_" before adding them to the HydroID field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How can I achieve the following using either Python Pivot Table or Pandas, thanks in advance for any help on the following.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Jul 2016 15:19:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420754#M33050</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2016-07-06T15:19:31Z</dc:date>
    </item>
    <item>
      <title>Re: Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420755#M33051</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Peter... if it just to facilitate flopping back and forth between rows and column primes, then you can just create an 'object' array by whipping in an extra row of column headers into your array structure then use the dtype object.&lt;/P&gt;&lt;P&gt;Note... this is a quick and dirty solution and just for the purposes designated, if you want to do any 'work' with the file, then ignore me&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; a = [["-","a","b","d","e"],["A",1,2,3,4],["B",5,6,7,8],["C",1,3,5,7]]
&amp;gt;&amp;gt;&amp;gt; a0 = np.asarray(a,dtype='object')
&amp;gt;&amp;gt;&amp;gt; a1 = a0.T
&amp;gt;&amp;gt;&amp;gt; a1
array([['-', 'A', 'B', 'C'],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ['a', 1, 5, 1],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ['b', 2, 6, 3],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ['d', 3, 7, 5],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ['e', 4, 8, 7]], dtype=object)
&amp;gt;&amp;gt;&amp;gt; a0
array([['-', 'a', 'b', 'd', 'e'],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ['A', 1, 2, 3, 4],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ['B', 5, 6, 7, 8],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ['C', 1, 3, 5, 7]], dtype=object)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:00:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420755#M33051</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T19:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420756#M33052</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Dan&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Apologies that I never got back to you sooner, as I've been off sick. Why can't you Pivot\Transpose a Structured Array? I have to forgo using &lt;A href="http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/tabletonumpyarray.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;TableToNumPyArray&lt;/A&gt;​ and use a &lt;A href="http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;SearchCursor&lt;/A&gt; to build a Python list from my &lt;A href="http://desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/zonal-histogram.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Zonal Histogram&lt;/A&gt; table results. I've achieved the pivoted results that I was looking for, but now need assistance getting the following back into a &lt;A href="http://docs.scipy.org/doc/numpy/user/basics.rec.html" rel="nofollow noopener noreferrer" target="_blank"&gt;Structured Array&lt;/A&gt; so that I can use &lt;A href="http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/numpyarraytotable.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;NumPyArrayToTable&lt;/A&gt;​:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''
Created on 06 Jul 2016

Zonal Histogram:

Pivot Results Table

@author: PeterW
'''
# import site-packages and modules
import numpy as np
import arcpy

# set arguments
hist_table = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_stats_160706"
output_pivot = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_pivot_160712"

# set environment settings
arcpy.env.overwriteOutput = True


# pivot zonal histogram results table
def hist_pivot(hist_table, output_pivot):
&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; fields = [f.name for f in arcpy.ListFields(hist_table)[1:]]
&amp;nbsp;&amp;nbsp;&amp;nbsp; fields_pivot = ["HydroID" if x == "LABEL" else int(x.replace("Hydro_", "")) for x in fields]
&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_list.append(fields_pivot)
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(hist_table, fields) as scur:# @UndefinedVariable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in scur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_list.append(list(row))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr = np.asarray(sum_list, dtype="object")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr_pivot = arr.T
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(arr_pivot)
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.da.NumPyArrayToTable(arr_pivot, output_pivot)# @UndefinedVariable

hist_pivot(hist_table, output_pivot)&lt;/PRE&gt;&lt;P&gt;Python Code&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/211461_pastedImage_2.png" style="max-width: 1200px; max-height: 900px;" /&gt;&lt;/P&gt;&lt;P&gt;Python Console: Print Statement of array transposed.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:00:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420756#M33052</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2021-12-11T19:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420757#M33053</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I will have a look again soon....&lt;/P&gt;&lt;P&gt;But, from my example, you will notice that the dtype is 'object'&amp;nbsp; which allows you to flip stuff around without having to worry about dtype.&lt;/P&gt;&lt;P&gt;So the trick (will confirm, done it a few times).&amp;nbsp; Is to retain the dtype of the input array brought in from Arc*, create a new view of the data with an 'object&amp;nbsp; dtype.&amp;nbsp; Do the 'stuff' and when done, rotate back and reapply the original dtype (or parts thereof). &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will revisit within the next day or so.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jul 2016 12:37:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420757#M33053</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-12T12:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420758#M33054</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Dan&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I solved it by using &lt;A href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.core.records.fromarrays.html" rel="nofollow noopener noreferrer" target="_blank"&gt;numpy.core.records.fromarrays&lt;/A&gt;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''
Created on 06 Jul 2016

Zonal Histogram Pivot:

Pivot Results Table

@author: PeterW
'''
# import site-packages and modules
import numpy as np
import arcpy

# set arguments
hist_table = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_stats_160706"
output_pivot = r"E:\Projects\2016\G112669\Calcs.gdb\ftr_watersheds_lu_pivot_v2_160712"

# set environment settings
arcpy.env.overwriteOutput = True


# pivot zonal histogram results table
def hist_pivot(hist_table, output_pivot):
&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; fields = [f.name for f in arcpy.ListFields(hist_table)[1:]]
&amp;nbsp;&amp;nbsp;&amp;nbsp; fields_pivot = ["HydroID" if x == "LABEL" else int(x.replace("Hydro_", "")) for x in fields]
&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_list.append(fields_pivot)
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(hist_table, fields) as scur:# @UndefinedVariable
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in scur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_list.append(list(row))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr = np.asarray(sum_list, dtype="object")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr_pivot = arr.T
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr_names = str(", ".join(arr_pivot[0]))
&amp;nbsp;&amp;nbsp;&amp;nbsp; dtypes_list = ["float64" for number in xrange(len(arr_pivot[0])-1)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; dtypes_list.insert(0, "int64")
&amp;nbsp;&amp;nbsp;&amp;nbsp; formats = str(", ".join(dtypes_list))
&amp;nbsp;&amp;nbsp;&amp;nbsp; rec_arr = np.core.records.fromarrays(arr_pivot[1:].transpose(), formats=formats, names = arr_names)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(rec_arr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.da.NumPyArrayToTable(rec_arr, output_pivot)# @UndefinedVariable

hist_pivot(hist_table, output_pivot)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Python Code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/211611_pastedImage_2.png" style="max-width: 1200px; max-height: 900px;" /&gt;&lt;/P&gt;&lt;P&gt;Python Pivot Results Table&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:00:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420758#M33054</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2021-12-11T19:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420759#M33055</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ohhh I misunderstood.... you forgot to do some reading from Numpy Repository&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;from numpy.lib._iotools import easy_dtype as easy&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;your tip of the day.&amp;nbsp; I have attached more readings.&amp;nbsp; Going to root is a good idea, but you have just accessed base recarray functionality... do know the surface names as well &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Attachment added&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jul 2016 19:09:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420759#M33055</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-12T19:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420760#M33056</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Truly appreciate your ongoing help and advice. &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jul 2016 19:45:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420760#M33056</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2016-07-12T19:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Pivot: Zonal Histogram Results Table</title>
      <link>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420761#M33057</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you.. it helped a lot&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Mar 2019 10:15:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pivot-zonal-histogram-results-table/m-p/420761#M33057</guid>
      <dc:creator>ShilpaRamesh</dc:creator>
      <dc:date>2019-03-29T10:15:42Z</dc:date>
    </item>
  </channel>
</rss>

