<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
df = pd.DataFrame(columns=[&quot;A&quot;, &quot;B&quot;, &quot;C&quot;])
df.set_index([&quot;A&quot;, &quot;B&quot;], inplace=True)
df.ix[(&quot;x&quot;, &quot;y&quot;), :] = 0

print(df)
print(df.ix[(&quot;x&quot;, slice(None)), :])
print(df)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">"A"</span>, <span class="pl-s">"B"</span>, <span class="pl-s">"C"</span>])
<span class="pl-s1">df</span>.<span class="pl-en">set_index</span>([<span class="pl-s">"A"</span>, <span class="pl-s">"B"</span>], <span class="pl-s1">inplace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">df</span>.<span class="pl-s1">ix</span>[(<span class="pl-s">"x"</span>, <span class="pl-s">"y"</span>), :] <span class="pl-c1">=</span> <span class="pl-c1">0</span>

<span class="pl-en">print</span>(<span class="pl-s1">df</span>)
<span class="pl-en">print</span>(<span class="pl-s1">df</span>.<span class="pl-s1">ix</span>[(<span class="pl-s">"x"</span>, <span class="pl-en">slice</span>(<span class="pl-c1">None</span>)), :])
<span class="pl-en">print</span>(<span class="pl-s1">df</span>)</pre></div>
<h5 dir="auto">Output</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="     C
A B   
x y  0
   C
B   
y  0
   C
B   
y  0"><pre class="notranslate"><code class="notranslate">     C
A B   
x y  0
   C
B   
y  0
   C
B   
y  0
</code></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">My understanding is that <code class="notranslate">DataFrame.ix</code> is a read-only operation and should not modify the <code class="notranslate">DataFrame</code>. However, in the example above column <code class="notranslate">A</code> is dropped from the index and thus cannot be use anymore.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">The unmodified  <code class="notranslate">DataFrame</code>. So the last print yields the same output as the first one.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
```
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.8.0-46-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: de_DE.UTF-8
<p dir="auto">pandas: 0.19.2<br>
nose: None<br>
pip: 9.0.1<br>
setuptools: 35.0.0<br>
Cython: None<br>
numpy: 1.12.1<br>
scipy: 0.19.0<br>
statsmodels: None<br>
xarray: None<br>
IPython: 5.3.0<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.0<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: None<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.1.9<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: None<br>
boto: None<br>
pandas_datareader: None</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;/details&gt;"><pre class="notranslate"><code class="notranslate">&lt;/details&gt;
</code></pre></div></details>
<h3 dir="auto">Small Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [13]: import pandas as pd
    ...:
    ...: df1 = pd.DataFrame(data=dict(A=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ...:                              B=[1, 1, 2, 2, 2, 3, 1, 1, 1, 2, 3, 4],
    ...:                              C=[1, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 4],
    ...:                              X=[1, 5, 2, 3, 8, 3, 3, 3, 1, 2, 1, 4],
    ...:                              Y=[7, 3, 4, 1, 3, 9, 9, 3, 1, 9, 3, 7]))
    ...: df1 = df1.set_index(['A', 'B', 'C'])
    ...:

In [14]: df1.loc[pd.IndexSlice[1, :, :]]
Out[14]:
     X  Y
B C
1 1  1  7
  2  5  3
2 1  2  4
  2  3  1
  3  8  3
..  .. ..
1 2  3  3
  3  1  1
2 2  2  9
3 1  1  3
4 4  4  7

[12 rows x 2 columns]

In [15]: df1
Out[15]:
     X  Y
B C
1 1  1  7
  2  5  3
2 1  2  4
  2  3  1
  3  8  3
..  .. ..
1 2  3  3
  3  1  1
2 2  2  9
3 1  1  3
4 4  4  7

[12 rows x 2 columns]"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">13</span>]: <span class="pl-s1">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
    ...:
    ...: <span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-en">dict</span>(<span class="pl-v">A</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>],
    ...:                              <span class="pl-v">B</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>],
    ...:                              <span class="pl-v">C</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>, <span class="pl-c1">4</span>],
    ...:                              <span class="pl-v">X</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">8</span>, <span class="pl-c1">3</span>, <span class="pl-c1">3</span>, <span class="pl-c1">3</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>, <span class="pl-c1">4</span>],
    ...:                              <span class="pl-v">Y</span><span class="pl-c1">=</span>[<span class="pl-c1">7</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">1</span>, <span class="pl-c1">3</span>, <span class="pl-c1">9</span>, <span class="pl-c1">9</span>, <span class="pl-c1">3</span>, <span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">3</span>, <span class="pl-c1">7</span>]))
    ...: <span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">df1</span>.<span class="pl-en">set_index</span>([<span class="pl-s">'A'</span>, <span class="pl-s">'B'</span>, <span class="pl-s">'C'</span>])
    ...:

<span class="pl-v">In</span> [<span class="pl-c1">14</span>]: <span class="pl-s1">df1</span>.<span class="pl-s1">loc</span>[<span class="pl-s1">pd</span>.<span class="pl-v">IndexSlice</span>[<span class="pl-c1">1</span>, :, :]]
<span class="pl-v">Out</span>[<span class="pl-c1">14</span>]:
     <span class="pl-v">X</span>  <span class="pl-v">Y</span>
<span class="pl-v">B</span> <span class="pl-v">C</span>
<span class="pl-c1">1</span> <span class="pl-c1">1</span>  <span class="pl-c1">1</span>  <span class="pl-c1">7</span>
  <span class="pl-c1">2</span>  <span class="pl-c1">5</span>  <span class="pl-c1">3</span>
<span class="pl-c1">2</span> <span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">4</span>
  <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">1</span>
  <span class="pl-c1">3</span>  <span class="pl-c1">8</span>  <span class="pl-c1">3</span>
..  .. ..
<span class="pl-c1">1</span> <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">3</span>
  <span class="pl-c1">3</span>  <span class="pl-c1">1</span>  <span class="pl-c1">1</span>
<span class="pl-c1">2</span> <span class="pl-c1">2</span>  <span class="pl-c1">2</span>  <span class="pl-c1">9</span>
<span class="pl-c1">3</span> <span class="pl-c1">1</span>  <span class="pl-c1">1</span>  <span class="pl-c1">3</span>
<span class="pl-c1">4</span> <span class="pl-c1">4</span>  <span class="pl-c1">4</span>  <span class="pl-c1">7</span>

[<span class="pl-c1">12</span> <span class="pl-s1">rows</span> <span class="pl-s1">x</span> <span class="pl-c1">2</span> <span class="pl-s1">columns</span>]

<span class="pl-v">In</span> [<span class="pl-c1">15</span>]: <span class="pl-s1">df1</span>
<span class="pl-v">Out</span>[<span class="pl-c1">15</span>]:
     <span class="pl-v">X</span>  <span class="pl-v">Y</span>
<span class="pl-v">B</span> <span class="pl-v">C</span>
<span class="pl-c1">1</span> <span class="pl-c1">1</span>  <span class="pl-c1">1</span>  <span class="pl-c1">7</span>
  <span class="pl-c1">2</span>  <span class="pl-c1">5</span>  <span class="pl-c1">3</span>
<span class="pl-c1">2</span> <span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">4</span>
  <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">1</span>
  <span class="pl-c1">3</span>  <span class="pl-c1">8</span>  <span class="pl-c1">3</span>
..  .. ..
<span class="pl-c1">1</span> <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">3</span>
  <span class="pl-c1">3</span>  <span class="pl-c1">1</span>  <span class="pl-c1">1</span>
<span class="pl-c1">2</span> <span class="pl-c1">2</span>  <span class="pl-c1">2</span>  <span class="pl-c1">9</span>
<span class="pl-c1">3</span> <span class="pl-c1">1</span>  <span class="pl-c1">1</span>  <span class="pl-c1">3</span>
<span class="pl-c1">4</span> <span class="pl-c1">4</span>  <span class="pl-c1">4</span>  <span class="pl-c1">7</span>

[<span class="pl-c1">12</span> <span class="pl-s1">rows</span> <span class="pl-s1">x</span> <span class="pl-c1">2</span> <span class="pl-s1">columns</span>]</pre></div>
<h3 dir="auto">Expected Output</h3>
<p dir="auto">The output of the slice <code class="notranslate">Out[14]</code> is correct, but <code class="notranslate">df1</code> should not be modified inplace. So the expected <code class="notranslate">Out[15]</code> is the original <code class="notranslate">df1</code>:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [17]: df1
Out[17]:
       X  Y
A B C
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
...   .. ..
  1 2  3  3
    3  1  1
  2 2  2  9
  3 1  1  3
  4 4  4  7

[12 rows x 2 columns]"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">17</span>]: <span class="pl-s1">df1</span>
<span class="pl-v">Out</span>[<span class="pl-c1">17</span>]:
       <span class="pl-v">X</span>  <span class="pl-v">Y</span>
<span class="pl-v">A</span> <span class="pl-v">B</span> <span class="pl-v">C</span>
<span class="pl-c1">1</span> <span class="pl-c1">1</span> <span class="pl-c1">1</span>  <span class="pl-c1">1</span>  <span class="pl-c1">7</span>
    <span class="pl-c1">2</span>  <span class="pl-c1">5</span>  <span class="pl-c1">3</span>
  <span class="pl-c1">2</span> <span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">4</span>
    <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">1</span>
    <span class="pl-c1">3</span>  <span class="pl-c1">8</span>  <span class="pl-c1">3</span>
...   .. ..
  <span class="pl-c1">1</span> <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">3</span>
    <span class="pl-c1">3</span>  <span class="pl-c1">1</span>  <span class="pl-c1">1</span>
  <span class="pl-c1">2</span> <span class="pl-c1">2</span>  <span class="pl-c1">2</span>  <span class="pl-c1">9</span>
  <span class="pl-c1">3</span> <span class="pl-c1">1</span>  <span class="pl-c1">1</span>  <span class="pl-c1">3</span>
  <span class="pl-c1">4</span> <span class="pl-c1">4</span>  <span class="pl-c1">4</span>  <span class="pl-c1">7</span>

[<span class="pl-c1">12</span> <span class="pl-s1">rows</span> <span class="pl-s1">x</span> <span class="pl-c1">2</span> <span class="pl-s1">columns</span>]</pre></div>
<hr>
<p dir="auto">I'm still not good at submitting issues here with code and print out, so I appreciate your patience.  Also, thank you guys for making pandas as amazing as it is!!</p>
<p dir="auto">Anyhow...</p>
<p dir="auto">I have dataframes that sometimes have up to 5 levels on their multiindex.  It's not uncommon for me to want to just grab a subset containing only one value on a certain level.  If one level of that index has only one value, then .loc can drop that level inplace.  I'd say this is highly undesirable.</p>
<p dir="auto">First the normal behavior.  Here's my input:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9
2 1 1  3  9
    2  3  3
    3  1  1
  2 2  2  9
  3 1  1  3
  4 4  4  7"><pre class="notranslate"><code class="notranslate">       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9
2 1 1  3  9
    2  3  3
    3  1  1
  2 2  2  9
  3 1  1  3
  4 4  4  7
</code></pre></div>
<p dir="auto">When I have a multi-indexed dataframe, and I do:<br>
df.loc[1]<br>
I get:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="     X  Y
B C      
1 1  1  7
  2  5  3
2 1  2  4
  2  3  1
  3  8  3
3 2  3  9"><pre class="notranslate"><code class="notranslate">     X  Y
B C      
1 1  1  7
  2  5  3
2 1  2  4
  2  3  1
  3  8  3
3 2  3  9
</code></pre></div>
<p dir="auto">I personally expect it to return the original multi-index where the first level has only that value.  Sadly, it drops it entirely ( I think this is terrible, since if you plan on resetting the index or concatenating later, you've just unwittingly lost information).</p>
<p dir="auto">Anyhow, I recognize now that you need to provide an index for all levels, e.g., the way I expected it to work can actually be achieved by (for a three level index):<br>
df.loc[pd.IndexSlice[1, :, :]]</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9"><pre class="notranslate"><code class="notranslate">       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9
</code></pre></div>
<p dir="auto">Here's the rub...  If the level that I indexed above has more than one unique value, this works fine.  If <strong>it has only one, then once again that level gets dropped, but worse, the index is modified in place during the .loc operation.</strong><br>
Here's the dataframe showing the bad behavior:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="       X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7"><pre class="notranslate"><code class="notranslate">       X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7
</code></pre></div>
<p dir="auto">df.loc[pd.IndexSlice[1, :, :]] gives:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7"><pre class="notranslate"><code class="notranslate">     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7
</code></pre></div>
<p dir="auto">Same syntax as the other case, but it dropped index A.  Worse is that this is now df.<br>
print(df)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7"><pre class="notranslate"><code class="notranslate">     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7
</code></pre></div>
<p dir="auto">If I modify the syntax slightly.  I.e., df.loc[pd.IndexSlice[1, :, :], :] (with the original not modifed frame, I get the expected result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="      X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7"><pre class="notranslate"><code class="notranslate">      X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7
</code></pre></div>
<p dir="auto">I've tried to provide a code sample with comments that demonstrates the problem.</p>
<h3 dir="auto">Code Sample, a copy-pastable example if possible</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd

df1 = pd.DataFrame(data=dict(A=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                             B=[1, 1, 2, 2, 2, 3, 1, 1, 1, 2, 3, 4],
                             C=[1, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 4],
                             X=[1, 5, 2, 3, 8, 3, 3, 3, 1, 2, 1, 4],
                             Y=[7, 3, 4, 1, 3, 9, 9, 3, 1, 9, 3, 7]))
df2 = df1.copy(deep=True)
df2['A'] = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]

df1 = df1.set_index(['A', 'B', 'C']).sortlevel()
df2 = df2.set_index(['A', 'B', 'C']).sortlevel()
df1_copy = df1.copy()

print(&quot;Here's df2, with more than 1 unique value for the index A:&quot;)
print(df2)

# already annoyed by this, I don't think this is how it should work, but I understand it
print(&quot;\nHere's what df2.loc[1] returns&quot;)
print(df2.loc[1])

# understand how to get around it at least
print(&quot;\nCan get around this annoyance by df2.loc[pd.IndexSlice[1, :, :]]&quot;)
print(df2.loc[pd.IndexSlice[1, :, :]])

# BUT!  If it's the only one...
print(&quot;\nHere's df1, with only a single value for the index A&quot;)
print(df1)

print(&quot;\nNow let's do the same thing we did for df2, namely display df1.loc[pidx[1, :, :]]&quot;)
print(df1.loc[pd.IndexSlice[1, :, :]])

# and holy crap it's an inplace operation!
print(&quot;\nDamnit.. it dropped by index again! And... ruh roh!  It has a side effect!  Here's df1 again:&quot;)
print(df1)

print(&quot;\nDoing df1.loc[pidx[1, :, :], :] (using the original df1) works as expected.&quot;)
print(df1_copy.loc[pd.IndexSlice[1, :, :], :])"><pre class="notranslate"><code class="notranslate">import pandas as pd

df1 = pd.DataFrame(data=dict(A=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                             B=[1, 1, 2, 2, 2, 3, 1, 1, 1, 2, 3, 4],
                             C=[1, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 4],
                             X=[1, 5, 2, 3, 8, 3, 3, 3, 1, 2, 1, 4],
                             Y=[7, 3, 4, 1, 3, 9, 9, 3, 1, 9, 3, 7]))
df2 = df1.copy(deep=True)
df2['A'] = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]

df1 = df1.set_index(['A', 'B', 'C']).sortlevel()
df2 = df2.set_index(['A', 'B', 'C']).sortlevel()
df1_copy = df1.copy()

print("Here's df2, with more than 1 unique value for the index A:")
print(df2)

# already annoyed by this, I don't think this is how it should work, but I understand it
print("\nHere's what df2.loc[1] returns")
print(df2.loc[1])

# understand how to get around it at least
print("\nCan get around this annoyance by df2.loc[pd.IndexSlice[1, :, :]]")
print(df2.loc[pd.IndexSlice[1, :, :]])

# BUT!  If it's the only one...
print("\nHere's df1, with only a single value for the index A")
print(df1)

print("\nNow let's do the same thing we did for df2, namely display df1.loc[pidx[1, :, :]]")
print(df1.loc[pd.IndexSlice[1, :, :]])

# and holy crap it's an inplace operation!
print("\nDamnit.. it dropped by index again! And... ruh roh!  It has a side effect!  Here's df1 again:")
print(df1)

print("\nDoing df1.loc[pidx[1, :, :], :] (using the original df1) works as expected.")
print(df1_copy.loc[pd.IndexSlice[1, :, :], :])
</code></pre></div>
<h4 dir="auto">Here's what I get from running the code</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Here's df2, with more than 1 unique value for the index A:
       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9
2 1 1  3  9
    2  3  3
    3  1  1
  2 2  2  9
  3 1  1  3
  4 4  4  7

Here's what df2.loc[1] returns
     X  Y
B C      
1 1  1  7
  2  5  3
2 1  2  4
  2  3  1
  3  8  3
3 2  3  9

Can get around this annoyance by df2.loc[pd.IndexSlice[1, :, :]]
       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9

Here's df1, with only a single value for the index A
       X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7

Now let's do the same thing we did for df2, namely display df1.loc[pidx[1, :, :]]
     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7

Damnit.. it dropped by index again! And... ruh roh!  It has a side effect!  Here's df1 again:
     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7

Doing df1.loc[pidx[1, :, :], :] (using the original df1) works as expected.
       X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7"><pre class="notranslate"><code class="notranslate">Here's df2, with more than 1 unique value for the index A:
       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9
2 1 1  3  9
    2  3  3
    3  1  1
  2 2  2  9
  3 1  1  3
  4 4  4  7

Here's what df2.loc[1] returns
     X  Y
B C      
1 1  1  7
  2  5  3
2 1  2  4
  2  3  1
  3  8  3
3 2  3  9

Can get around this annoyance by df2.loc[pd.IndexSlice[1, :, :]]
       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9

Here's df1, with only a single value for the index A
       X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7

Now let's do the same thing we did for df2, namely display df1.loc[pidx[1, :, :]]
     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7

Damnit.. it dropped by index again! And... ruh roh!  It has a side effect!  Here's df1 again:
     X  Y
B C      
1 1  1  7
  1  3  9
  2  5  3
  2  3  3
  3  1  1
2 1  2  4
  2  3  1
  2  2  9
  3  8  3
3 1  1  3
  2  3  9
4 4  4  7

Doing df1.loc[pidx[1, :, :], :] (using the original df1) works as expected.
       X  Y
A B C      
1 1 1  1  7
    1  3  9
    2  5  3
    2  3  3
    3  1  1
  2 1  2  4
    2  3  1
    2  2  9
    3  8  3
  3 1  1  3
    2  3  9
  4 4  4  7
</code></pre></div>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">What I expect from all of the examples above, is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9"><pre class="notranslate"><code class="notranslate">       X  Y
A B C      
1 1 1  1  7
    2  5  3
  2 1  2  4
    2  3  1
    3  8  3
  3 2  3  9
</code></pre></div>
<h4 dir="auto">output of <code class="notranslate">pd.show_versions()</code></h4>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 2.7.11.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.6.3-300.fc24.x86_64<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_US.UTF-8</p>
<p dir="auto">pandas: 0.18.1<br>
nose: None<br>
pip: 8.0.3<br>
setuptools: 20.1.1<br>
Cython: 0.23.4<br>
numpy: 1.10.4<br>
scipy: 0.17.0<br>
statsmodels: None<br>
xarray: None<br>
IPython: 4.1.2<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.4.2<br>
pytz: 2015.7<br>
blosc: None<br>
bottleneck: None<br>
tables: 3.2.2<br>
numexpr: 2.5.1<br>
matplotlib: 1.5.0<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.0.10<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: None<br>
pandas_datareader: None</p>