在pivot_table,Pandas中排列列

时间:2013-06-19 16:14:05

标签: python pandas pivot-table

我有一个关于在pivot_table中重新排列列的问题。我希望按月对列进行分组,但安排如下:

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

1 个答案:

答案 0 :(得分: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

首先按switching the column levels,然后sorting by columns

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