Python pandas slice dataframe by multiple index ranges
<p>What is the pythonic way to slice a dataframe by more index ranges (eg. by <code>10:12</code> and <code>25:28</code>)?
I want this in a more elegant way:</p>

<pre><code>df = pd.DataFrame({'a':range(10,100)})
df.iloc[[i for i in range(10,12)] + [i for i in range(25,28)]]
</code></pre>

<p>Result:</p>

<pre><code>     a
10  20
11  21
25  35
26  36
27  37
</code></pre>

<p>Something like this would be more elegant:</p>

<pre><code>df.iloc[(10:12, 25:28)]
</code></pre>

<p>Thank you!</p>

<p>You can use numpy's <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html" rel="nofollow">r_</a> "slicing trick":</p>

<pre><code>df = pd.DataFrame({'a':range(10,100)})
df.iloc[pd.np.r_[10:12, 25:28]]
</code></pre>

<p>Gives:</p>

<pre><code>     a
10  20
11  21
25  35
26  36
27  37
</code></pre>

python pandas indexing slice