微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 重塑Pandas中的数据帧

是否有快速pythonic方式来转换此表

index = pd.date_range('2000-1-1', periods=36, freq='M')
df = pd.DataFrame(np.random.randn(36,4), index=index, columns=list('ABCD'))


In[1]: df

Out[1]: 
                   A         B         C         D
2000-01-31         H  1.368795  0.106294  2.108814
2000-02-29 -1.713401  0.557224  0.115956 -0.851140
2000-03-31 -1.454967 -0.791855 -0.461738 -0.410948
2000-04-30  1.688731 -0.216432 -0.690103 -0.319443
2000-05-31 -1.103961  0.181510 -0.600383 -0.164744
2000-06-30  0.216871 -1.018599  0.731617 -0.721986
2000-07-31  0.621375  0.790072  0.967000  1.347533
2000-08-31  0.588970 -0.360169  0.904809  0.606771
...

进入这张桌子

                       2001                                2000            
            12 11 10 9 8 7 6 5 4 3 2 1        12 11 10 9 8 7 6 5 4 3 2 1 
A                                                                      H
B
C
D

请原谅缺失的值.我手动添加了“H”.我希望它能说清楚我在寻找什么.

解决方法:

为了便于检查,我创建了相同形状的数据框,但是以整数作为值.

解决方案的核心是pandas.DataFrame.transpose,但您需要使用index.year index.month作为新索引:

>>> df = pd.DataFrame(np.random.randint(10,size=(36, 4)), index=index, columns=list('ABCD'))
>>> df.set_index(keys=[df.index.year, df.index.month]).transpose()
  2000                                  2001                                  2002                                 
    1  2  3  4  5  6  7  8  9  10 11 12   1  2  3  4  5  6  7  8  9  10 11 12   1  2  3  4  5  6  7  8  9  10 11 12
A    0  0  8  7  8  0  7  1  5  1  5  4    2  1  9  5  2  0  5  3  6  4  9  3    5  1  7  3  1  7  6  5  6  8  4  1
B    4  9  9  5  2  0  8  0  9  5  2  7    5  6  3  6  8  8  8  8  0  6  3  7    5  9  6  3  9  7  1  4  7  8  3  3
C    3  2  4  3  1  9  7  6  9  6  8  6    3  5  3  2  2  1  3  1  1  2  8  2    2  6  9  6  1  5  6  5  4  6  7  5
D    8  1  3  9  2  3  8  7  3  2  1  0    1  3  9  1  8  6  4  7  4  6  3  2    9  8  9  9  0  7  4  7  3  6  5  2

当然,如果每月有多个记录,这将无法正常工作.在这种情况下,您首先需要groupby数据:

>>> i = pd.date_range('2000-1-1', periods=36, freq='W') # weekly index
>>> df = pd.DataFrame(np.random.randint(10,size=(36, 4)), index=i, columns=list('ABCD'))
>>> df.groupby(by=[df.index.year, df.index.month]).sum().transpose()
  2000                               
     1   2   3   4   5   6   7   8  9
A   12  13  15  23   9  21  21  31  7
B   33  24  19  30  15  19  20   7  4
C   20  24  26  24  15  18  29  17  4
D   23  29  14  30  19  12  12  11  5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐