MultiIndex Slicing requires the index to be fully lexsorted : <p>I have a data frame with index (<code>year</code>, <code>foo</code>), where I would like to select the X largest observations of <code>foo</code> where <code>year == someYear</code>.</p>

<p>My approach was </p>

<pre><code>df.sort_index(level=[0, 1], ascending=[1, 0], inplace=True)
df.loc[pd.IndexSlice[2002, :10], :]
</code></pre>

<p>but I get</p>

<pre><code>KeyError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (2), lexsort depth (0)'
</code></pre>

<p>I tried different variants of sorting (e.g. <code>ascending = [0, 0]</code>), but they all resulted in some sort of error.</p>

<p>If I only wanted the <code>xth</code> row, I could <code>df.groupby(level=[0]).nth(x)</code> after sorting, but since I want a set of rows, that doesn't feel quite efficient.</p>

<p>What's the best way to select these rows? Some data to play with:</p>

<pre><code>                   rank_int  rank
year foo                         
2015 1.381845             2   320
     1.234795             2   259
     1.148488           199     2
     0.866704             2   363
     0.738022             2   319
</code></pre>
