2

我有一个关于重新排列数据透视表中的列的问题。我想按月对列进行分组,但安排如下:

JAN      FEB  
X,Y,X/Y  X,Y,X/Y ....

当前输出为:

JAN FEB      JAN  FEB    JAN ...

X   X   ...  Y     Y ...  X/Y ...

在构建具有多列的数据透视表时,我注意到在 Excel 中实现的相同行为。

请参阅下面的示例。输出具有第一种格式。谢谢

from pandas import DataFrame,pivot_table
import numpy as np
from datetime import datetime 

names=["a","b","c","a","b"]
dates=["20/01/2013","21/01/2013","22/02/2013", "01/03/2013","01/03/2013"]
dico={"x":[1,3,5,7,9], "y":[2,4,6,8,10], "date":dates, "name":names}

df=DataFrame(dico)
df["month"]=[datetime.strptime(x,'%d/%m/%Y').month for x in dates ]

print df
mpivot=pivot_table(df, values=["x","y"],cols=["month"], rows="name",aggfunc=np.sum)
print mpivot
4

1 回答 1

4

创建此数据透视表后,您可以执行此操作:

In [11]: p = pivot_table(df, values=["x","y"], cols=["month"], 
                             rows="name", aggfunc=np.sum)

In [12]: p
Out[12]:
        x           y
month   1   2   3   1   2   3
name
a       1 NaN   7   2 NaN   8
b       3 NaN   9   4 NaN  10
c     NaN   5 NaN NaN   6 NaN

首先通过切换列级别,然后按列排序

In [13]: p.reorder_levels([1, 0], axis=1).sort_index(axis=1)
Out[13]:
month   1       2       3
        x   y   x   y   x   y
name
a       1   2 NaN NaN   7   8
b       3   4 NaN NaN   9  10
c     NaN NaN   5   6 NaN NaN
于 2013-06-20T09:21:03.590 回答