Skip to content

Instantly share code, notes, and snippets.

@mindey
Last active Oct 20, 2017
Embed
What would you like to do?
# Practically, it's useful if we have complex observations (rows) and variables (columns):
df = pandas.DataFrame(
data=pandas.np.array(
[[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15]]).T,
index=pandas.MultiIndex.from_arrays(
[['x','x','x','y','z'],
['a','a','b','b','c'],
[1,2,1,2,3]],
names=['level1', 'level2', 'level3']),
columns=pandas.MultiIndex.from_arrays(
[['VAR', 'VAR', 'VAR'],
['VAR-A','VAR-A', 'VAR-B'],
['var-a','var-b', 'var-c']],
names=['LEVEL1', 'LEVEL2', 'LEVEL3']
)
)
LEVEL1 VAR
LEVEL2 VAR-A VAR-B
LEVEL3 var-a var-b var-c
level1 level2 level3
x a 1 1 6 11
2 2 7 12
b 1 3 8 13
y b 2 4 9 14
z c 3 5 10 15
# This allows us to filter by levels of indexing.
df[df.index.get_level_values('level2').isin(['a', 'c'])]
LEVEL1 VAR
LEVEL2 VAR-A VAR-B
LEVEL3 var-a var-b var-c
level1 level2 level3
x a 1 1 6 11
2 2 7 12
z c 3 5 10 15
# If we want to select an intersection from multiple levels, obviously, we can do:
ix1 = df.index.get_level_values('level2').isin(['a', 'c'])
ix2 = df.index.get_level_values('level3').isin([1, 3])
df[ix1 & ix2]
LEVEL1 VAR
LEVEL2 VAR-A VAR-B
LEVEL3 var-a var-b var-c
level1 level2 level3
x a 1 1 6 11
z c 3 5 10 15
cx1 = df.columns.get_level_values('LEVEL2').isin(['VAR-A'])
cx2 = df.columns.get_level_values('LEVEL3').isin(['var-b'])
df.T[cx1 & cx2].T
LEVEL1 VAR
LEVEL2 VAR-A
LEVEL3 var-b
level1 level2 level3
x a 1 6
2 7
b 1 8
y b 2 9
z c 3 10
# Selecting by both:
df[ix1 & ix2].T[cx1 & cx2].T
LEVEL1 VAR
LEVEL2 VAR-A
LEVEL3 var-b
level1 level2 level3
x a 1 6
z c 3 10
# Examples of slicing such frames:
https://pandas.pydata.org/pandas-docs/stable/advanced.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment