add a field in pandas dataframe with MultiIndex columns

后端 未结 4 721
长发绾君心
长发绾君心 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:13

    This is a decade old but I had the exact same problem. here is a 1 line way to do what you are looking for. pandas 0.18 as been introduce so rolling mean is a bit different now, but you get the point.

    avg_close = dftst.xs('close', axis=1, level=1).rolling(5).mean()   
    dftst[zip(avg_close.columns, ['avg_close']*len(avg_close.columns))] = avg_close
    

提交回复
热议问题