How to exclude values from pandas dataframe?
<p>I have two dataframes:</p>

<p>1) customer_id,gender
2) customer_id,...[other fields]</p>

<p>The first dataset is an answer dataset (gender is an answer). So, I want to exclude from the second dataset those customer_id which are in the first dataset (which gender we know) and call it 'train'. The rest records should become a 'test' dataset.</p>

<p>I think you need <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a> and condition with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html" rel="nofollow"><code>isin</code></a>, inverting <code>boolean Series</code> is by <code>~</code>:</p>

<pre><code>df1 = pd.DataFrame({'customer_id':[1,2,3],
                   'gender':['m','f','m']})

print (df1)
   customer_id gender
0            1      m
1            2      f
2            3      m

df2 = pd.DataFrame({'customer_id':[1,7,5],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df2)
   B  C  D  E  F  customer_id
0  4  7  1  5  7            1
1  5  8  3  3  4            7
2  6  9  5  6  3            5
</code></pre>



<pre><code>mask = df2.customer_id.isin(df1.customer_id)
print (mask)
0     True
1    False
2    False
Name: customer_id, dtype: bool

print (~mask)
0    False
1     True
2     True
Name: customer_id, dtype: bool

train = df2[mask]
print (train)
   B  C  D  E  F  customer_id
0  4  7  1  5  7            1

test  = df2[~mask]
print (test)
   B  C  D  E  F  customer_id
1  5  8  3  3  4            7
2  6  9  5  6  3            5
</code></pre>

python pandas