Search code examples
pythonpandaspandas-groupbymulti-index

Hierarchical Operation Pandas


Clearly I'm missing something simple but I don't know what. I would like to propagate an operation by groups. Let's say something simple, I have a simple series with multiindex (let's say 2 levels), I want to take the mean and subtract that mean to the correct index level.

Minimalist example code:

a = pd.Series({(2,1): 3., (1,2):4.,(2,3):4.})
b = a.groupby(level=0).mean()
r = a-b # this is the wrong line, b doesn't propagate to the multiindex of a

The result that I expect:

2  1    -0.5
1  2    0
2  3    .5
dtype: float64

Solution

  • Use Series.sub with possible defined level for align:

    r = a.sub(b, level=0)
    print (r)
    2  1   -0.5
    1  2    0.0
    2  3    0.5
    dtype: float64
    

    Or use GroupBy.transform for Series with same index like original a Series:

    b = a.groupby(level=0).transform('mean')
    r = a-b
    print (r)
    2  1   -0.5
    1  2    0.0
    2  3    0.5
    dtype: float64