That's what the cookbook recipe provides, a function called summary() that makes a pivot table. Problem solved :-)
> I should be clear that my complaint is with > Python rather than the code as such.
There are plenty of ways to write the summary() function with plain, straight-forward Python code that doesn't use generators, itertools, or any other advanced feature.
So, why does the recipe author use itertools? It is because they provide a way to get C speed without having to write extension modules. Had the author used map() instead of a generator expression, the inner-loop would run entirely at C speed (with no trips around the Python eval-loop):
for pivot_value, row in groupby(data, key):
yield k, sum(map(value, group))
I think it's wonderful that a two-line helper function is all it takes to implement pivot tables efficiently.