Search code examples
pythonpandasdataframelevels

Python Perform Custom Calculations on 0 level columns


I have a Data Frame:

import pandas as pd

df = pd.DataFrame({
    ('A', 'a'): [1, 2, 3],
    ('A', 'b'): [4, 5, 6],
    ('B', 'a'): [7, 8, 9],
    ('B', 'b'): [10, 11, 12],
})
df

I would like to perform a defined calculation on each of the columns in each Zero Level and output another Zero Level label "C".

I would like to do "A" * "B" / 2

resulting in a Data Frame output:

df = pd.DataFrame({
    ('A', 'a'): [1, 2, 3],
    ('A', 'b'): [4, 5, 6],
    ('B', 'a'): [7, 8, 9],
    ('B', 'b'): [10, 11, 12],
    ('C', 'a'): [3.5, 8, 13.5],
    ('C', 'b'): [20, 27.5, 36],
})
df

My initial thought process was to do a .groupby on level=0, axis=1 then use .apply() with a function. Thanks.


Solution

  • With stack/unstack and eval :

    out = df.stack().eval("C = A * B / 2").unstack()
    

    Output :

    print(out)
    
       A     B         C
       a  b  a   b     a     b
    0  1  4  7  10   3.5  20.0
    1  2  5  8  11   8.0  27.5
    2  3  6  9  12  13.5  36.0