add a field in pandas dataframe with MultiIndex columns

后端 未结 4 773
长发绾君心
长发绾君心 2021-02-06 00:00

i have looked for an answer to this question as it seems pretty simple, but have not been able to find anything yet. Apologies if I missed something. I have pandas version 0.1

4条回答
  •  轮回少年
    2021-02-06 00:16

    You could also (as a workaround since there isn't really an API that does exactly what you want ) consider a bit of reshaping-fu if you don't want to use a Panel. I wouldn't recommend it on enormous data sets, though: use a Panel for that.

    In [30]: df = dftst.stack(0)
    
    In [31]: df['close_avg'] = pd.rolling_mean(df.close.unstack(), 5).stack()
    
    In [32]: df
    Out[32]: 
    field                          close      rate  close_avg
                        ticker                               
    2009-03-01 06:29:59 AAPL   -0.223042  0.554996        NaN
                        GOOG    0.060127 -0.333992        NaN
                        GS      0.117626 -1.256790        NaN
    2009-03-02 06:29:59 AAPL   -0.513743 -0.402661        NaN
                        GOOG    0.059828 -0.125288        NaN
                        GS     -0.336196 -0.510595        NaN
    2009-03-03 06:29:59 AAPL    0.142202 -1.038470        NaN
                        GOOG   -1.099251 -0.892581        NaN
                        GS      1.698086  0.885023        NaN
    2009-03-04 06:29:59 AAPL   -1.125821  0.413005        NaN
                        GOOG    0.424290  1.106983        NaN
                        GS      0.047158  0.680714        NaN
    2009-03-05 06:29:59 AAPL    0.470050  1.845354  -0.250071
                        GOOG    0.132956 -0.488800  -0.084410
                        GS      0.129190  0.208077   0.331173
    2009-03-06 06:29:59 AAPL   -0.087360 -2.102512  -0.222934
                        GOOG    0.165100 -0.134886  -0.063415
                        GS      0.167720  0.082480   0.341192
    2009-03-07 06:29:59 AAPL   -0.768542 -0.176076  -0.273894
                        GOOG    0.417694  2.257074   0.008158
                        GS     -1.744730 -1.850185   0.059485
    2009-03-08 06:29:59 AAPL   -0.297363 -0.633828  -0.361807
                        GOOG   -1.096703 -0.572138   0.008667
                        GS      0.890016 -2.621563  -0.102129
    2009-03-09 06:29:59 AAPL    1.038579  0.053330   0.071073
                        GOOG   -0.614050  0.607944  -0.199001
                        GS     -0.882848  0.596801  -0.288130
    2009-03-10 06:29:59 AAPL   -0.255226  0.058178  -0.073982
                        GOOG    1.761861  1.841751   0.126780
                        GS     -0.549998 -1.551281  -0.423968
    2009-03-11 06:29:59 AAPL    0.413522  0.149089   0.026194
                        GOOG   -2.964163  1.825312  -0.499072
                        GS     -0.373303  1.137001  -0.532173
    2009-03-12 06:29:59 AAPL   -0.924776  1.238546  -0.005053
                        GOOG   -0.985956 -0.906590  -0.779802
                        GS     -0.320400  1.239681  -0.247307
    

提交回复
热议问题