Pivot Table/Groupby/Sum question

patrick.waldo at gmail.com patrick.waldo at gmail.com
Thu Dec 27 18:05:33 EST 2007


On Dec 27, 10:59 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Dec 28, 4:56 am, patrick.wa... at gmail.com wrote:
>
> > from itertools import groupby
>
> You seem to have overlooked this important sentence in the
> documentation: "Generally, the iterable needs to already be sorted on
> the same key function"

Yes, but I imagine this shouldn't prevent me from using and
manipulating the data.  It also doesn't explain why the names get
sorted correctly and the time does not.

I was trying to do this:

count_tot = []
for k, g in groupby(data, key=lambda r: r[NAME]):
    for record in g:
        count_tot.append((k,record[SALARY]))
for i in count_tot:
here I want to say add all the numbers for each person, but I'm
missing something.

If you have any ideas about how to solve this pivot table issue, which
seems to be scant on Google, I'd much appreciate it.  I know I can do
this in Excel easily with the automated wizard, but I want to know how
to do it myself and format it to my needs.





More information about the Python-list mailing list