Python list or pandas dataframe arbitrary indexing and slicing
<p>I have used both R and Python extensively in my work, and at times I get the syntax between them confused.</p>

<p>In R, if I wanted to create a model from only <strong><em>some</em></strong> features of my data set, I can do something like this:</p>

<pre><code>subset = df[1:1000, c(1,5,14:18,24)]
</code></pre>

<p>This would take the first 1000 rows (yes, R starts on index 1), and it would take the 1st, 5th, 14th <strong><em>through</em></strong> 18th, and 24th columns.</p>

<p>I have tried to do any combination of <code>slice</code>, <code>range</code>, and similar sorts of functions, and have not been able to duplicate this sort of flexibility.  In the end, I just enumerated all of the values.</p>

<p>How can this be done in Python?</p>

<blockquote>
  <p>Pick an arbitrary subset of elements from a list, some of which are selected individually (as in the commas shown above) and some selected sequentially (as in the colons shown above)?</p>
</blockquote>

<p>In a file of <code>index_tricks</code>, <code>numpy</code> defines a class instance that converts a scalars and slices into an enumerated list, using the <code>r_</code> method:</p>

<pre><code>In [560]: np.r_[1,5,14:18,24]
Out[560]: array([ 1,  5, 14, 15, 16, 17, 24])
</code></pre>

<p>It's an instance with a <code>__getitem__</code> method, so it uses the indexing syntax.  It expands <code>14:18</code> into <code>np.arange(14,18)</code>.  It can also expand values with <code>linspace</code>.</p>

<p>So I think you'd rewrite</p>

<pre><code>subset = df[1:1000, c(1,5,14:18,24)]
</code></pre>

<p>as</p>

<pre><code>df.iloc[:1000, np.r_[0,4,13:17,23]]
</code></pre>

python pandas numpy slice