Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Indexing a multiindex using a boolean series

I have a Series with a multiindex of companies and filing periods. I want to index this series with a separate boolean series of companies that pass some filter, returning all companies and filing periods that match the list of companies provided.

Here is example code:
First create a series with a multiindex.

arrays = [
    ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
    ["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
s = pd.Series(np.random.randn(8), index=index)

first  second
bar    one      -0.107011
       two       1.170427
baz    one      -0.567174
       two      -0.902847
foo    one      -0.986784
       two       0.972722
qux    one       0.714136
       two       0.044249
dtype: float64

Then create the boolean series (in my case generated from a groupby object with a filter for companies with multiple filing periods):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test = pd.Series([True, False, True, False], index=['bar', 'baz', 'foo', 'qux'], name='first')
bar     True
baz    False
foo     True
qux    False
Name: first, dtype: bool

I want to slice the Series to only return rows that match company bar and foo and omit the rest.

s.loc[test]
>>>IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

Can you help?

>Solution :

Try:

>>> s[test[test].index]
first  second
bar    one      -0.188867
       two       0.025214
foo    one       0.693238
       two       2.569597
dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading