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>

<p>Another handy trick is <code>to_frame()</code></p>

<pre><code>df['col3'].to_frame()
</code></pre>

python pandas sklearn-pandas