简体   繁体   中英

Python Pandas- how to unstack a pivot table with two values with each value becoming a new column?

After pivoting a dataframe with two values like below:

import pandas as pd

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                       'foo', 'bar', 'foo', 'bar'],
            'B' : ['one', 'one', 'two', 'two',
                      'two', 'two', 'one', 'two'],
            'C' : [56, 2, 3, 4, 5, 6, 0, 2],
            'D' : [51, 2, 3, 4, 5, 6, 0, 2]})

pd.pivot_table(df, values=['C','D'],rows='B',cols='A').unstack().reset_index()

When I unstack the pivot and reset the index two new columns 'level_0' and 0 are created. Level_0 contains the column names C and D and 0 contains the values.

    level_0     A   B   0
0   C   bar     one     2.0
1   C   bar     two     4.0
2   C   foo     one     28.0
3   C   foo     two     4.0
4   D   bar     one     2.0
5   D   bar     two     4.0
6   D   foo     one     25.5
7   D   foo     two     4.0

Is it possible to unstack the frame so each value (C,D) appears in a separate column or do I have to split and concatenate the frame to achieve this? Thanks.

edited to show desired output:

    A   B   C   D
0   bar one 2   2
1   bar two 4   4
2   foo one 28  25.5
3   foo two 4   4

You want to stack (and not unstack):

In [70]: pd.pivot_table(df, values=['C','D'],rows='B',cols='A').stack()
Out[70]: 
          C     D
B   A            
one bar   2   2.0
    foo  28  25.5
two bar   4   4.0
    foo   4   4.0

Although the unstack you used did a 'stack' operation because you had no MultiIndex in the index axis (only in the column axis).

But actually, you can get there also (and I think more logical) with a groupby-operation , as this is what you actually do (group columns C and D by A and B):

In [72]: df.groupby(['A', 'B']).mean()
Out[72]: 
          C     D
A   B            
bar one   2   2.0
    two   4   4.0
foo one  28  25.5
    two   4   4.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM