Hi Dan
I've used the code example within your "Observation_Summary_1.pdf" and understand the most of your code, but if you wouldn't mind unpacking from Line 45 to 50. I'm unsure if line 45 is a list comprehension of some sort and not understanding the how its working.
# coding: utf-8
"""
Dan Patterson:
Numpy Pivot Table Summary
16/05/2016
"""
# import site-packages and modules
import numpy as np
import arcpy
# set input summary table
input_table = r"E:\Projects\2016\G112224\Models\Schools\Schools_Combined_160505.gdb\De_Villiers_Graaff_Hs_SAA_Stats"
output_gdb = r"E:\Python\Testing\dan_patterson_numpy\SAA_Summary_Report_Testing.gdb"
# numpy pivot table function
def pivot_summary(input_table, output_gdb):
# convert summary table to structured numpy array
numpy_fields = ("OBJECTID", "TOWN", "SETTLEMENTNAME",
"NAME", "TIME", "FREQUENCY")
sum_array = arcpy.da.TableToNumPyArray(input_table, numpy_fields) # @UndefinedVariable
# obtain unique records based on first three columns
unique_records = np.unique(sum_array[['TOWN', 'SETTLEMENTNAME', 'NAME']])
# number of unique rows
shp = len(unique_records)
# construct the output array
dt = [('TOWN', 'U20'), ('SETTLEMENTNAME', 'U20'), ('NAME', 'U20'),
('TIME5', np.int32), ('TIME10', np.int32), ('TIME15', np.int32),
('TIME20', np.int32), ('TIME25', np.int32), ('TIME30', np.int32),
('TIME60', np.int32)]
# populate array with zeros
pivot_array = np.zeros(shp, dtype=dt)
# assign the first three columns
pivot_array['TOWN'] = unique_records['TOWN']
# the values from the unique test
pivot_array['SETTLEMENTNAME'] = unique_records['SETTLEMENTNAME']
# everything is sorted
pivot_array['NAME'] = unique_records['NAME']
# loop through unique records array
for i in range(shp):
# pull out the rows that match
row_match = sum_array[sum_array[['TOWN', 'SETTLEMENTNAME',
'NAME']] == unique_records]
for j in range(len(row_match)):
column = 'TIME' + str(row_match['TIME'])
buildings = row_match['FREQUENCY']
pivot_array[column] = buildings
pivot_table = "{0}\\{1}".format(output_gdb, "Pivot_Table_Summary")
arcpy.da.NumPyArrayToTable(pivot_array, pivot_table) # @UndefinedVariable
return pivot_table
pivot_table = pivot_summary(input_table, output_gdb)
Thanks for your help Dan