[Numpy-discussion] Slicing and structured arrays question

Travis Oliphant oliphant at enthought.com
Tue Jan 27 10:06:52 EST 2009


Hanno Klemm wrote:
> Hi, 
> I have the following question, that I could not find an answer to in the
> example list, or by googling:
>
> I have a record array with dtype such as:
> dtype([('times', '<f8'), ('sensors', '|S8'), ('prop1', '<f8'), ('prop2',
> '<f8'), ('prop3', '<f8'), ('prop4', '<f8')])
>
> I would now like to calculate the mean and median for each of the
> properties 'props'. Is there a way to do this similarly to a conventional
> array with
>
> a[:,2:].mean(axis=0)
>
> or do I have to use a loop over the names of the properties? 
>   
You must first view the array as a float array in order to calculate the 
mean:

This should work:

b = a.view(float)
b.shape = (-1,6)
b[:,2:].mean(axis=0)


-Travis




More information about the NumPy-Discussion mailing list