Skip to content
Advertisement

Pandas multiIndex slicing by aggregate

I have a pandas Series (S) that has an index like:

JavaScript

I have a conditional function that returns a lower dimensional index. What I am doing is performing something like S.groupby(level=(0,1)).median() > 1

This returns a Series with Index like so:

JavaScript

How do I slice the original Series with the lower dimensional index?

I know I can reset index and select rows using .isin but I would like to use MultiIndex if possible.

Thanks in advance!

===

Here is what the actual Series (s) looks like:

JavaScript

I perform the following operation:

JavaScript

Now sms looks like:

JavaScript

Now I want to slice the series s that match the index in sms only.

So I want this slice of s (that matches the index of sms):

JavaScript

Advertisement

Answer

It’s possible only if your index levels are the same which is not the case here because in s, you have ['BATCH', 'ITEM', 'SEQ', 'X', 'Y'] and in sms, you have only ['BATCH', 'ITEM', 'SEQ'].

You have to drop X and Y levels before to match indexes:

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