Suppose I have the following MultiIndex DataFrame, titled df:
arrays = [["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
["one", "two", "one", "two", "one", "two", "one", "two"],]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
df = pd.Series(np.random.randn(8), index=index)
If I wanted to access all the rows associated with baz, for example, I would use cross-section: df.xs(('baz')).
But is there a way to access the rows by referencing the integer location in the first level, similar to iloc for single index DataFrames? In my example, I think that would be index location 1.
I attempted it with a workaround using .loc as per the following:
(df.loc[[df.index.get_level_values(0)[1]]]
But that returns the first group of rows associated with bar. Which I believe its because integer-location 1 is still within bar. I would have to reference 2 to get to baz.
Can I make it so that location 0, 1, 2, and 3 references bar, baz, foo, and qux respectively?