When indexing a multi-channel DataFrame, it looks like it .ilocassumes that you are referring to the "internal level" of the index, but .loclooking at the external level.
For instance:
np.random.seed(123)
iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
idx = pd.MultiIndex.from_product(iterables, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(8, 4), index=idx)
print(df.loc['qux'])
0 1 2 3
second
one -1.25388 -0.63775 0.90711 -1.42868
two -0.14007 -0.86175 -0.25562 -2.79859
print(df.iloc[-1])
0 -0.14007
1 -0.86175
2 -0.25562
3 -2.79859
Name: (qux, two), dtype: float64
Two questions:
First, why is this? Is this a deliberate design decision?
Secondly, can I use .ilocto refer to the external level of the index to get the result below? I know that at first I can find the last element of the index c get_level_values, and then .loc-index with this, but wandering if it can be done more directly, either with funk syntax .ilocor with some existing function designed specifically for the case.
# df.iloc[-1]
qux one 0.89071 1.75489 1.49564 1.06939
two -0.77271 0.79486 0.31427 -1.32627