【问题标题】:Arranging columns in pivot_table, Pandas在pivot_table,Pandas中排列列
【发布时间】:2013-06-19 16:14:04
【问题描述】:

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

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

【问题讨论】:

  • 我添加了一个例子
  • 谢谢! (顺便说一句,您可以使用 pd.to_datetime(col, day_first=True) 转换为日期时间:))

标签: python pandas pivot-table


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 2016-01-22
    • 2018-08-01
    • 2017-04-28
    • 1970-01-01
    • 2018-07-03
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多