Embedding a range in a pandas series
<p>I have a table of 14 columns and I want to pull select ones into a new dataframe. 
Let's say I want column 0 then column 8-14</p>

<pre><code>  dfnow = pd.Series([df.iloc[row_count,0], \
                    df.iloc[row_count,8], \
                    df.iloc[row_count,9], \
                    ....
</code></pre>

<p>Works but seems clumsy</p>

<p>I'd like to write </p>

<pre><code>  dfnow = pd.Series([df.iloc[row_count,0], \
          df.iloc[row_count, range (8, 14)]])
</code></pre>

<p>But this throws a ValueError: Wrong number of items passed</p>

<p>Now, from the answer below, I know I can create two separate sereis and concatenate them, but that seems a little sub-optimal as well. </p>

<p><a href="https://stackoverflow.com/questions/12504493/adding-pandas-series-with-different-indices-without-getting-nans">Adding pandas Series with different indices without getting NaNs</a></p>

<p>consider the <code>df</code></p>

<pre><code>from string import ascii_uppercase
import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(150).reshape(-1, 15),
                  columns=list(ascii_uppercase[:15]))
df
</code></pre>

<p><a href="https://i.stack.imgur.com/84S4N.png" rel="nofollow"><img src="https://i.stack.imgur.com/84S4N.png" alt="enter image description here"></a></p>

<p>use <code>np.r_</code> to construct the array neccesary for the slice you want</p>

<pre><code>np.r_[0, 8:14]

array([ 0,  8,  9, 10, 11, 12, 13])
</code></pre>

<p>then slice</p>

<pre><code>df.iloc[:, np.r_[0, 8:14]]
</code></pre>

<p><a href="https://i.stack.imgur.com/RBwdB.png" rel="nofollow"><img src="https://i.stack.imgur.com/RBwdB.png" alt="enter image description here"></a></p>

python pandas