具有3种多索引级别的Pandas数据透视表

时间:2020-02-07 18:52:13

标签: pandas pivot-table multi-index

我已经尽一切可能尝试采用此数据帧

import pandas as pd

dataDict = {'State': ['Idaho', 'Wyoming', 'Montana', 'Idaho', 'Idaho', 'Wyoming', 'Montana', 'Idaho', 'Idaho', 'Wyoming', 'Montana', 'Idaho'],
            'City': ['Boise', 'Jackson', 'Missoula', 'Sandpoint', 'Boise', 'Jackson', 'Missoula', 'Sandpoint', 'Boise', 'Jackson', 'Missoula', 'Sandpoint'],
            'Years': [2010, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012],
            'PizzaOrdered' : [3000, 50, 1000, 78, 3250, 75, 1250, 82, 4000, 98, 4100, 92],
            'TacosOrdered' : [5000, 65, 1900, 88, 5780, 78, 2128, 90, 6125, 87, 5999, 95]
            }
testData = pd.DataFrame(data=dataDict)

enter image description here

并将其转换为此

enter image description here

我已经尝试过枢轴,groupby,set_index,堆叠,拆堆,并且我可以接近但不安静,我在下面提供的这个示例

2 个答案:

答案 0 :(得分:2)

melt之前,您需要unstack

df_final = (testData.melt(['State', 'City', 'Years'], var_name='Ordered')
                    .set_index(['State', 'City', 'Ordered', 'Years'])['value']
                    .unstack())

Out[54]:
Years                           2010  2011  2012
State   City      Ordered
Idaho   Boise     PizzaOrdered  3000  3250  4000
                  TacosOrdered  5000  5780  6125
        Sandpoint PizzaOrdered    78    82    92
                  TacosOrdered    88    90    95
Montana Missoula  PizzaOrdered  1000  1250  4100
                  TacosOrdered  1900  2128  5999
Wyoming Jackson   PizzaOrdered    50    75    98
                  TacosOrdered    65    78    87

答案 1 :(得分:1)

您可以先执行pivot_table,然后执行'stack

testData.pivot_table(index=['State','City'], columns='Years').stack(0)

输出:

Years                           2010  2011  2012
State   City                                    
Idaho   Boise     PizzaOrdered  3000  3250  4000
                  TacosOrdered  5000  5780  6125
        Sandpoint PizzaOrdered    78    82    92
                  TacosOrdered    88    90    95
Montana Missoula  PizzaOrdered  1000  1250  4100
                  TacosOrdered  1900  2128  5999
Wyoming Jackson   PizzaOrdered    50    75    98
                  TacosOrdered    65    78    87