title: Slice Non-Contiguous and Contiguous Columns in Pandas to the Last Column in DataFrame
body: <p>I am fairly new to python and want to get non-contiguous columns in pandas, but can seem to figure it out. I know in R it could be done like <code>df[:, c(1, 3:)]</code> to select columns 1, 3 to end of columns when using indexing. Just want to know how that is done in python using a general approach that would be applicable to different datasets with differing number of columns</p>
  <p>Say I have generate some data like below:</p>
  <pre><code>## generate integer and category/hierarchy data
  dataset = pd.DataFrame({'Group': np.random.choice(range(1, 5), 100, replace=True),
                          &quot;y&quot;: np.random.choice(range(1, 6), 100, replace=True),
                          &quot;X1&quot;: np.random.choice(range(1, 6), 100, replace=True),
                          &quot;X2&quot;: np.random.choice(range(1, 6), 100, replace=True),
                          &quot;X3&quot;: np.random.choice(range(1, 6), 100, replace=True),
                          &quot;X4&quot;: np.random.choice(range(1, 6), 100, replace=True),
                          &quot;X5&quot;: np.random.choice(range(1, 6), 100, replace=True)
                        })
  dataset.head()
  </code></pre>
  <p>I know I can select columns 0 and 1 (Group and y) with <code>dataset.iloc[:, np.r_[0,1]]</code>, and I can also select columns <code>Group, X1 through X5</code> with <code>dataset.iloc[:, np.r_[0, 2:7]]</code>.</p>
  <pre><code>    Group   X1      X2          X3         X4   X5
  0   2   3.000000    4.000000    5.000000    4.0 2.0
  1   2   4.000000    2.000000    2.000000    5.0 3.0
  2   1   5.000000    1.000000    3.000000    5.0 1.0
  3   4   5.000000    2.986855    2.000000    3.0 4.0
  4   1   1.000000    3.000000    5.000000    4.0 1.0
  ... ... ... ... ... ... ...
  95  1   3.000000    3.000000    2.000000    5.0 3.0
  96  4   2.964054    4.000000    5.000000    1.0 5.0
  97  2   4.000000    3.000000    2.863587    2.0 5.0
  98  1   3.000000    3.000000    4.000000    3.0 2.0
  99  4   5.000000    2.692210    3.000000    3.0 1.0
  </code></pre>
  <p>My question is, is there a more general way to select columns <code>2:</code> to the last column using the <code>np.r_</code> function, like can be done in R <code> df[:, c(1, 3:)]</code>.</p>