Pivot Table/Groupby/Sum question

John Machin sjmachin at lexicon.net
Thu Dec 27 19:48:12 EST 2007


On Dec 28, 10:05 am, patrick.wa... at gmail.com wrote:
> 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.

You imagine correctly (and pointlessly) in general; however in
particular it prevents you using itertools.groupby simplistically to
manipulate the data in the way you want to manipulate it.

> It also doesn't explain why the names get
> sorted correctly and the time does not.

The names in your example were NOT sorted, "correctly" or otherwise.
The output order is the same as the input order: Bob, Joe, Jil.

>>> seq = ['Bob', 'Joe', 'Jil']
>>> sorted(seq)
['Bob', 'Jil', 'Joe']
>>> seq == sorted(seq)
False
>>>

>
> 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.

Watch this space.





More information about the Python-list mailing list