Pandas Group By,使用行和列中的索引定义输出

时间:2015-12-01 14:22:28

标签: python pandas

目前我有一些数据,其中回报按月和年分组,但在输出中,想法是在列中有几年的行和月(如我在Quantopian中找到的图像)。

enter image description here

目前我所做的是:

import pandas as pd
import numpy as np

dates = pd.date_range('20130101', periods=5000)
df = pd.DataFrame(np.random.randn(5000,2) , columns=['ret', 'trend'], index = dates)

df2 = pd.DataFrame()
df2['ret'] = df.groupby([lambda x: x.year, lambda x: x.month], as_index=True)['ret'].sum()
print df2

               ret
2013 1   -0.888623
     2   -1.920381
     3   -2.253708
     4   -8.316681
     5   -2.426211
     6   -5.889560
     7   -4.072581
     8    0.669934
     9  -11.944923
     10  -1.429509
     11  -6.705022
     12   4.413347
2014 1   -2.819433
     2   -9.309527
     3    4.164549
     4    4.316265
     5   -6.489879
     6    0.599227
     7   -8.679500
     8   -7.031178
     9   -3.849903
     10   0.036255
     11   7.148089
     12   2.167552
2015 1   13.957739
     2   -4.248225
     3   -0.224388
     4    0.909705
     5   -6.561675
     6    0.207894
...            ...
2024 4    5.368534
     5   -5.405187
     6   -3.548288
     7   -1.253442
     8    3.510136
     9    0.964269
     10   4.044782
     11  -0.802436
     12  -2.461015
2025 1   -1.381566
     2   -3.026784
     3    1.402344
     4   -6.962486
     5    1.810279
     6   -2.330699
     7    2.624671
     8   -2.810254
     9   -5.865352
     10  20.853629
     11  -1.642405
     12   3.810005
2026 1   -6.433632
     2    0.345212
     3   -5.052783
     4    2.568629
     5   -0.031563
     6   -4.391030
     7   -6.286675
     8    2.329209
     9   -3.506839

[165 rows x 1 columns]

如何在数月内成为此输出的列?

1 个答案:

答案 0 :(得分:2)

您可以致电unstack并传递level=1以取消堆叠特定级别:

In [124]:
df2.unstack(level=1)

Out[124]:
           ret                                                         \
            1          2          3          4          5          6    
2013 -9.868992  -4.346157   3.866546  -8.626669   5.139571   6.279008   
2014 -1.535937  -7.150076  12.477616   3.913517  -3.305470  -2.865723   
2015  4.895234  -6.265481  -6.898884   0.097624  12.565113   2.195401   
2016 -4.526340  -2.936200  -5.912908   2.729341   0.815636  -4.912806   
2017 -1.546016   2.473249  -4.205015   4.102542  -4.458827   1.924468   
2018  6.475616   0.027515   2.638676  -6.600986  -2.903825  14.835129   
2019 -6.147727  -1.927974   2.574085 -11.910082  -1.879927   6.275029   
2020  1.888112   4.597125   3.380548  -3.843723   2.419507  -1.427722   
2021  1.004944   0.180754  -8.159088   9.789561  -1.085590   6.248256   
2022 -9.635066  -0.772752   1.445861  -5.435704  -0.863745  -2.939604   
2023 -1.161640   0.488229  -1.607350   0.228962   4.987129   7.192585   
2024 -1.183226  10.776812  -5.597332   3.118547  -0.655470  -0.738184   
2025  3.962616 -11.581283  -1.021046  -0.440242   1.403144  -7.224395   
2026 -1.977520   6.571101   0.003448   0.191040  -7.202345  -8.193564   


            7          8         9          10        11         12  
2013  2.959460  10.185449 -8.537985   4.659689  5.088191 -10.974003  
2014 -0.327318   0.456162  1.528933  17.446656 -7.539484 -13.894860  
2015 -3.673257  -5.406524  5.557376   2.645820  1.034233  -6.025324  
2016 -3.028106   0.193528 -1.870737  -7.610312  4.926571  -0.229560  
2017 -1.311885   3.934379 -3.117008   1.500675 -6.710537  -7.828733  
2018  5.751018  -2.329534  9.277459  -6.410702 -2.633642  -2.590974  
2019 -9.765319 -13.551314 -4.538661   2.563119 -2.949883  -3.896485  
2020  2.356527   4.775241 -1.428101  -5.261007  9.322340  -1.350198  
2021  0.509185  -4.233567  4.262016  -7.600269  6.084152  -0.804232  
2022  2.236611  -0.506173 -2.528419   0.518371  8.969728   1.970115  
2023  5.951624  -2.948382  0.814657  -5.907658 -2.376326   8.222634  
2024 -1.422230   4.984618 -1.196657  -4.488551 -2.509809  -6.104347  
2025 -1.306259   0.326205 -0.748180  -0.627567  8.017221   8.684555  
2026 -4.524792   0.101453 -3.360516        NaN       NaN        NaN