[Numpy-discussion] How to implement a 'pivot table?'

Geoffrey Zhu zyzhu2000@gmail....
Mon Jul 30 10:12:06 CDT 2007


Hi Everyone,

I am wondering what is the best (and fast) way to build a pivot table
aside from the 'brute force way?'

I want to transform an numpy array into a pivot table. For example, if
I have a numpy array like below:

Region     Date          # of Units
----------    ----------        --------------
East        1/1             10
East        1/1             20
East        1/2             30
West       1/1             40
West       1/2             50
West       1/2             60

I want  to transform this into the following table, where f() is a
given aggregate function:

           Date
Region           1/1          1/2
----------
East         f(10,20)         f(30)
West        f(40)             f(50,60)


I can regroup them into 'sets' and do it the brute force way, but that
is kind of slow to execute. Does anyone know a better way?


Thanks,
Geoffrey


More information about the Numpy-discussion mailing list