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

标签 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

我尝试过pivot、groupby、set_index、stacking、unstacking,我可以接近但不安静我在下面提供的这个例子

最佳答案

unstack之前需要melt

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

关于具有 3 级多索引的 Pandas 数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60119439/

相关文章:

postgresql - 数据透视表/交叉表 : I wan to return dynamic columns

python - 替换 MultiIndex (pandas) 中的一个值

pandas - 如何解决read_csv中的Unicode错误?

python - 如何将聚合函数应用于 Pandas 中数据透视表的所有列

python - 使用 pandas python reshape 为二进制变量

python - 从多索引 pandas 数据框中选择索引和列的子集

python - 根据索引将大型数据框中的数据除以较小数据框中的数据

python - 转置和组合 panda 数据框

python - 如何查找 Pandas 系列中出现特定次数的值?

python - sklearn.cross_validation.StratifiedShuffleSplit - 错误 : "indices are out-of-bounds"