Consider the following code:
import pandas as pd
d_foo = {}
d_foo[('a',1,'w')] = 1
d_foo[('b',2,'x')] = 1
d_foo[('c',2,'y')] = 1
d_foo[('d',3,'z')] = 1
df = pd.Series(d_foo)
df_sel = df[:,2,:]
This results in the following Series:
0
b x 1
c y 1
Now say instead of 2, I want to get all rows that have either 2 or 3 in the second position like so:
0
b x 1
c y 1
d z 1
Initially I tried:
df_sel = df[:,[2,3],:]
However this yielded the following error:
TypeError: '[2, 3]' is an invalid key
Based on this thread I tried the answer to 2b:
df_sel = df[pd.IndexSlice[:,[2,3]],:]
However this yielded the same error as above.
Is it possible to select all rows with key 2 and 3 in the index in an elegant way?