5

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.

1 Answer 1

6

Try:

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

Alternately:

fdf.pivot_table('percent', rows='row', cols='col', aggfunc='sum').values
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.