You Are What You Decide: A Journey in Automation of Our Selves. |
Suppose we have a multi-indexed dataframe, and want to select all observations with certain ids in a certain level or levels.
df = pd.DataFrame(np.arange(15).reshape(5,3), index=[['x','x','x','y','z'], ['a','a','b','b','c'], [1,2,1,2,3]], columns=[['VAR', 'VAR', 'VAR'], ['VAR-A','VAR-A', 'VAR-B'], ['var-a','var-b', 'var-c']] ) df.index.names = ['one', 'two', 'three']
df VAR VAR-A VAR-B var-a var-b var-c one two three x a 1 0 1 2 2 3 4 5 b 1 6 7 8 y b 2 9 10 11 z c 3 12 13 14 [5 rows x 3 columns]
Here's one way to do it:
df[df.index.get_level_values('two').isin(['a', 'c'])] VAR VAR-A VAR-B var-a var-b var-c one two three x a 1 0 1 2 2 3 4 5 z c 3 12 13 14 [3 rows x 3 columns]
If we want to select an intersection from multiple levels, obviously, we can do:
ix1 = df.index.get_level_values('two').isin(['a', 'c']) ix2 = df.index.get_level_values('three').isin([1, 3]) df[ix1 & ix2] VAR VAR-A VAR-B var-a var-b var-c one two three x a 1 0 1 2 z c 3 12 13 14 [2 rows x 3 columns]