Why df[[2,3,4]][2:4] works and df[[2:4]][2:4] does not in Python
<p>suppose we have a datarame</p>

<pre><code>import pandas as pd
df = pd.read_csv('...')
df
  0 1 2 3 4
0 1 2 3 4 5
1 1 2 3 4 5
2 1 2 3 4 5
3 1 2 3 4 5
4 1 2 3 4 5
</code></pre>

<p>Why one approach is working and other returns syntax error?</p>

<p>It fails because <code>2:4</code> is invalid syntax for accessing the keys/columns of a df:</p>

<pre><code>In [73]:
df[[2:4]]
  File "&lt;ipython-input-73-f0f09617b349&gt;", line 1
    df[[2:4]]
         ^
SyntaxError: invalid syntax
</code></pre>

<p>This is no different to if you defined a dict and tried the same syntax:</p>

<pre><code>In [74]:
d = {0:0,1:1,2:2,3:3,4:4,5:5}
d

Out[74]:
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}

In [76]:
d[[2:4]]

  File "&lt;ipython-input-76-ea5d68adc389&gt;", line 1
    d[[2:4]]
        ^
SyntaxError: invalid syntax
</code></pre>

<p>The <code>[]</code> syntax is used to access column labels that match, you can't pass a slice in a list to access a range of columns like this, it needs to be a list of values as you've already found</p>

<p>The newer methods such as <code>iloc</code>, <code>ix</code> and <code>loc</code> support slice ranges</p>

<p>What worked for you, initially selected the columns using the labels in a list:</p>

<pre><code>In [77]:
df[[2,3,4]]

Out[77]:
   2  3  4
0  3  4  5
1  3  4  5
2  3  4  5
3  3  4  5
4  3  4  5
</code></pre>

<p>And then selected the rows via a slice:</p>

<pre><code>In [79]:
df[[2,3,4]][2:4]

Out[79]:
   2  3  4
2  3  4  5
3  3  4  5
</code></pre>

python pandas dataframe subset