Get dataframe columns from a list using isin
<p>I have a dataframe <code>df1</code>, and I have a list which contains names of several columns of <code>df1</code>.</p>

<pre><code>df1:
User_id  month  day  Age   year    CVI    ZIP    sex  wgt
0           1    7   16    1977     2      NA    M    NaN
1           2    7   16    1977     3      NA    M    NaN
2           3    7   16    1977     2      DM    F    NaN
3           4    7   16    1977     7      DM    M    NaN
4           5    7   16    1977     3      DM    M    NaN
...        ...    ...  ...   ...   ...     ...  ...  ...
35544      35545   12   31  2002    15      AH  NaN  NaN
35545      35546   12   31  2002    15      AH  NaN  NaN
35546      35547   12   31  2002    10      RM    F   14
35547      35548   12   31  2002     7      DO    M   51
35548      35549   12   31  2002     5     NaN  NaN  NaN

 list= [u"User_id", u"day", u"ZIP", u"sex"]
</code></pre>

<p>I want to make a new dataframe <code>df2</code> which will contain omly those columns which are in the list, and a dataframe <code>df3</code> which will contain columns which are not in the list.</p>

<p><a href="http://stackoverflow.com/questions/16804476/python-pandas-isin-on-a-list">Here</a> I found that I need to do:</p>

<pre><code>df2=df1[df1[df1.columns[1]].isin(list)]
</code></pre>

<p>But as a result I get:</p>

<pre><code>Empty DataFrame
Columns: []
Index: []
[0 rows x 9 columns]
</code></pre>

<p>What Im I odoing wrong and how can i get a needed result? Why "9 columns" if it supossed to be 4?</p>

<p>never name a list as "list"</p>

<pre><code>my_list= [u"User_id", u"day", u"ZIP", u"sex"]
df2 = df1[df1.keys()[df1.keys().isin(my_list)]]
</code></pre>

python list pandas condition multiple-columns