Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas aggregated data to a numpy array : data structure conversion

Tags:

python

pandas

I have aggregated data using pandas data frame. Below is some actual data shown and how I aggregated it.

fdf.groupby(['row',col'])['percent'].sum()

http://pastebin.com/R8XWpgtU

What I would like to do is create a 2d numpy array of this (rows = row, columns = col). Any slick way to do this ?

Another way I did something similar was create a pivot table

pivot_table(fdf,values='percent',rows='row',cols='col', aggfunc=np.sum)

In this case I want to convert this pivot table to 2d numpy array. Is there a way for me to index into each cell of this table. If so then I probably will be Ok with the table itself.

like image 814
Abhi Avatar asked Apr 18 '12 18:04

Abhi


1 Answers

Try:

result = fdf.groupby(['row',col'])['percent'].sum()
result.unstack('col').values

Alternately:

fdf.pivot_table('percent', rows='row', cols='col', aggfunc='sum').values
like image 127
Wes McKinney Avatar answered Sep 19 '22 22:09

Wes McKinney