Python Perform Custom Calculations on 0 level columns

Question:

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.

Asked By: Evan

||

Answers:

You can just do the calculation you want – i.e, (df['A'] * df['B']).div(2) and assign the values to new columns in the original DataFrame.

df[[('C', 'a'), ('C', 'b')]] = (df['A'] * df['B']).div(2)

   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
Answered By: It_is_Chris

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
Answered By: Timeless

Here is another way:

pd.concat([df,pd.concat([df.groupby(level=1,axis=1).prod().div(2)],keys =['C'],axis=1)],axis=1)

Output:

   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
Answered By: rhug123
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.