Summing over a multiindex level in a pandas series

前端 未结 1 969
遇见更好的自我
遇见更好的自我 2021-01-31 08:22

Using the Pandas package in python, I would like to sum (marginalize) over one level in a series with a 3-level multiindex to produce a series with a 2 level multiindex. For exa

1条回答
  •  迷失自我
    2021-01-31 09:06

    If you know you always want to aggregate over the first two levels, then this is pretty easy:

    In [27]: data.groupby(level=[0, 1]).sum()
    Out[27]:
    A  B    277
       b     37
    a  B    159
       b     16
    dtype: int64
    

    0 讨论(0)
提交回复
热议问题