python & pandas - Drop rows where column values are index values in another DataFrame
<p>The Original DataFrame(<code>df1</code>) looks like:</p>

<pre><code>  NoUsager Sens NoAdresse Fait  Weekday NoDemande  Periods
 0 000001   +    000079    1     Dim   42191000972 Soir 
 1 001875   +    005018    1     Dim   42191001052 Matin 
 2 001651   +    005018    1     Dim   42191001051 Matin 
 3 001486   +    000405    1     Dim   42191001250 Matin 
 4 002021   +    005712    1     Dim   42191000013 Matin 
 5 001975   +    005712    1     Dim   42191000012 Matin 
 6 001304   +    001408    1     Dim   42191000371 Matin 
 7 001355   +    005021    1     Dim   42191000622 Matin 
 8 002274   +    006570    1     Dim   42191001053 Matin 
 9 000040   +    004681    1     Dim   42191002507 Soir
</code></pre>

<p>I used <code>crosstab</code> to generate a new one(<code>df2</code>) with <code>index = NoDemande, NoUsager, Periods</code> and <code>columns = ['Sens']</code>:</p>

<pre><code>                       Sens  + - 
NoDemande  NoUsager Periods
42191000622 001355  Matin    1 2 
42191000959 001877  Matin    1 2 
42191001325 000627  Soir     1 2 
42191001412 000363  Matin    1 2 
42191001424 000443  Soir     1 2 
42191001426 001308  Soir     1 2 
42191002507 000040  Soir     2 0 
42193000171 000257  Soir     1 2 
42193000172 002398  Soir     1 2 
</code></pre>

<p>I want to drop all the rows from <code>df1</code> where values in columns <code>NoUsager</code> and <code>NoDemande</code> are the same as the one in index <code>NoUsager</code> and <code>NoDemande</code> in <code>df2</code>. So the result will return a new DataFrame<code>df3</code> with the same <code>df1</code> format  but without <code>line7</code> and <code>line9</code>.</p>

<p>I tried: </p>

<pre><code>df3 = df1.loc[~df1['NoDemande','NoUsager'].isin([df2.NoDemande,df2.NoUsager])]
</code></pre>

<p>But it returned: <code>KeyError: ('NoDemande', 'NoUsager')</code></p>

<p>How can I solve this problem?</p>

<p>Any help will be appreciated! </p>

<pre><code>cols = ['NoDemande','NoUsager']
mask = df1[cols].isin(df2.reset_index()[cols].to_dict('list'))
df1[~mask.all(1)]
</code></pre>

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

<hr>

<p>There were three things you were doing incorrectly.</p>

<ol>
<li><p><code>df1['NoDemande','NoUsager']</code> needs to be <code>df1[['NoDemande','NoUsager']]</code></p></li>
<li><p><code>df2</code> has index levels with names <code>['NoDemande','NoUsager']</code>.  You must reset the index to turn them back into columns.</p></li>
<li><p>When using <code>isin</code> for this purpose, transform <code>df2.reset_index()[['NoDemande','NoUsager']]</code> into a dictionary.</p></li>
</ol>

python pandas dataframe