Skip to content
Advertisement

How to Efficiently Perform Multiplication within MultiIndex Groupby

I am trying to use two of my second level indices to calculate a third index. However, I can’t find an idiomatic way to do this.

How can I calculate one second level index from two other second level indices? Each group has the same second level indices.

My Code

JavaScript

This produces the following data frame:

JavaScript

What I Have

Note that I know that I will need to do some work with indexes to get the below to work, but would rather find a better way if one exists rather than using this code.

JavaScript

Is there a better way to do this?

Advertisement

Answer

Try xs as an alternative to pd.IndexSlice where you get to remove one level, then mul which allows level alignment when multiply:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement