Well... who needs an advanced license or excel ... Assume we have an array of the format shown in the Input section. We can determine the counts or sums of unique values in a field, using the following. - sort the array on a field,
- get unique values in that field,
- sum using the values in another field as weights
- rotate if desired
import numpy as np
a =
a_s = a[np.argsort(a, order="Class")]
a_u, idx = np.unique(a_s["Class"], return_inverse=True)
bin_cnt = np.bincount(idx,weights=a_s['Count'])
ans = np.array((a_u, bin_cnt), dtype='object')
print("a_u\n{}\nidx {}\nanswer\n{}".format(a_u, idx, ans))
rot90 = np.rot90(ans, k=1)
and_flipud = np.flipud(rot90)
frmt = ("pivot table... rotate 90, flip up/down\n{}"
print(frmt.format(and_flipud))
The trick is to set the data type to 'object'. You just use FeatureClassToNumPyArray or TableToNumPyArray and their inverses to get to/from array format. Ergo....pivot table should NOT be just for an advanced license For all-ish combos, you can just add the desired lines to the above for i in range(4):
print("\nrotated {}\n{}".format(90*i, np.rot90(a, k=i)))
for i in range(4):
f = "\nrotate by {} and flip up/down\n{}"
print(f.format(90*i, np.flipud(np.rot90(a, k=i))))
for i in range(5):
f = "\nrotate by {} and flip left/right\n{}"
print(f.format(90*i, np.fliplr(np.rot90(a, k=i))))
| Input table with the following fields 'ID', 'X', 'Y', 'Class', 'Count' >>> input array...
[[(0, 6.0, 0.0, 'a', 10)]
[(1, 7.0, 9.0, 'c', 1)]
[(2, 8.0, 6.0, 'b', 2)]
[(3, 3.0, 2.0, 'a', 5)]
[(4, 6.0, 0.0, 'a', 5)]
[(5, 2.0, 5.0, 'b', 2)]
[(6, 3.0, 2.0, 'a', 10)]
[(7, 8.0, 6.0, 'b', 2)]
[(8, 7.0, 9.0, 'c', 1)]
[(9, 6.0, 0.0, 'a', 10)]]
>>>
a_u
['a' 'b' 'c']
idx [0 0 0 0 0 1 1 1 2 2]
answer
[['a' 'b' 'c']
[40.0 6.0 2.0]]
pivot table... rotate 90, flip up/down
[['a' 40.0]
['b' 6.0]
['c' 2.0]]
|