How to keep one single column as a dataframe
<p>I have dataframe with 20 columns and one index.</p>

<p>Its shape is something like (100, 20).</p>

<p>I want to slice the 3rd column from this dataframe, but want to keep the result as a dataframe of (100,1). </p>

<ol>
<li>If I do a <code>v = df['col3']</code>, I get a Series (which I do not want)</li>
<li>If I do a <code>v =df[df['col3']!=0]</code> and then <code>v.drop(label=[list of 19 columns], axis = 1)</code> --- I get what I want [that is a df(100,1)] but I have to </li>
</ol>

<p>(a) write an unnecessary != condition ( which I want to avoid) and </p>

<p>(b) I have to write a long list of 19 column names.</p>

<p>There should be a better and cleaner way of doing what I wan to achieve.</p>

<blockquote>
  <p>If I do a <code>v = df['col3']</code>, I get a Series (which I do not want)</p>
</blockquote>

<p>If you use <code>df[cols]</code>, where <code>cols</code> is a list, you'll get a DataFrame (not a Series). This includes the case where it is a list consisting of a single item. So, you can use <code>df[['col3']]</code>.</p>

<p>For example:</p>

<pre><code>In [33]: df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
</code></pre>

<p>This gives a Series:</p>

<pre><code>In [35]: df['a']
Out[35]: 
0    1
1    2
Name: a, dtype: int64
</code></pre>

<p>This gives a DataFrame:</p>

<pre><code>In [36]: df[['a']]
Out[36]: 
   a
0  1
1  2
</code></pre>

<p>Finally, note that you can always transform a Series to a DataFrame with <code>reset_index</code>. So here, you can also use:</p>

<pre><code>In [44]: df['a'].reset_index()
Out[44]: 
   index  a
0      0  1
1      1  2
</code></pre>

python pandas sklearn-pandas