I have a multiindex dataframe like this:
import pandas as pd
import numpy as np
df = pd.DataFrame({'ind1': list('aaaaaaaaabbbbbbbbb'),
'ind2': list('cccdddeeecccdddeee'),
'ind3': list(range(3))*6,
'val1': list(range(100, 118)),
'val2': list(range(70, 88))})
df_mult = df.set_index(['ind1', 'ind2', 'ind3'])
val1 val2
ind1 ind2 ind3
a c 0 100 70
1 101 71
2 102 72
d 0 103 73
1 104 74
2 105 75
e 0 106 76
1 107 77
2 108 78
b c 0 109 79
1 110 80
2 111 81
d 0 112 82
1 113 83
2 114 84
e 0 115 85
1 116 86
2 117 87
I can now select a subset of it using .loc like this
df_subs = df_mult.loc['a', ['c', 'd'], :]
which gives the expected
val1 val2
ind1 ind2 ind3
a c 0 100 70
1 101 71
2 102 72
d 0 103 73
1 104 74
2 105 75
If I now want to select again a subset of df_subs, e.g.
df_subs.loc['a', 'c', :]
works and gives
val1 val2
ind3
0 100 70
1 101 71
2 102 72
however
df_subs.loc[:, 'c', :]
fails and gives an error
KeyError: 'the label [c] is not in the [columns]'
Why does this fail?
EDIT
Originally, I had two question in this post. I splitted it into two, the second question can be found here.