python 2.7 - Pandas Multi-indexing from a Flatten DataFrame -


i want resample flatten dataframe multi-indexed columns. dataframe looks :

goods category month stock       c1     1     5       c1     2     0       c1     3     0       c2     1     0       c2     2    10       c2     3     0 b       c1     1    30 b       c1     2     0 b       c1     3    10 b       c2     1     0 b       c2     2    40 b       c2     3     0 

and set him :

stock             goods              b     category    c1  c2  c1  c2 month                      1            5   0  30   0 2            5  10  30  40 3            5  10  10  40 

i try somethings groupby or stack don't find way...does know how ?

with unstack (to use first have set multi-index):

in [48]: df.set_index(['goods', 'category', 'month']).unstack([0,1]) out[48]:          stock goods              b category    c1  c2  c1  c2 month 1            5   0  30   0 2            0  10   0  40 3            0   0  10   0 

alternative pivot_table (but aware, if have multiple values same combination of goods/category/month, averaged default (another function can specified)):

in [54]: df.pivot_table(columns=['goods', 'category'], index='month', values='stock') out[54]: goods           b category c1  c2  c1  c2 month 1         5   0  30   0 2         0  10   0  40 3         0   0  10   0 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -