Removing values from dataframe based on groupby condition
<p>I'm a bit stuck here trying to determine how to slice my dataframe.</p>

<pre><code>data = {'Date' : ['08/20/10','08/20/10','08/20/10','08/21/10','08/22/10','08/24/10','08/25/10','08/26/10'] , 'Receipt' : [10001,10001,10002,10002,10003,10004,10004,10004],
   'Product' : ['xx1','xx2','yy1','fff4','gggg4','fsf4','gggh5','hhhg6']}

dfTest = pd.DataFrame(data)
dfTest
</code></pre>

<p>This will produce:</p>

<pre><code>    Date    Product    Receipt
0   08/20/10    xx1    10001
1   08/20/10    xx2    10001
2   08/20/10    yy1    10002
3   08/21/10    fff4    10002
4   08/22/10    gggg4   10003
5   08/24/10    fsf4    10004
6   08/25/10    gggh5   10004
7   08/26/10    hhhg6   10004
</code></pre>

<p>I want to create a new dataframe that only contains unique receipts, meaning the receipt should only be used on 1 day only (but it can be shown multiple times in 1 day). If the receipt shows up in multiple days, it needs to be removed.  The above data set should look like this:</p>

<pre><code>    Date    Product    Receipt
0   08/20/10    xx1    10001
1   08/20/10    xx2    10001
2   08/22/10    gggg4   10003
</code></pre>

<p>What I have done so far is:</p>

<pre><code>dfTest.groupby(['Receipt','Date']).count()

              Product
Receipt Date    
10001   08/20/10    2
10002   08/20/10    1
        08/21/10    1
10003   08/22/10    1
10004   08/24/10    1
        08/25/10    1
        08/26/10    1
</code></pre>

<p>I didn't know how to do a query for that date in that kind of structure, so I reset the index.</p>

<pre><code>df1 = dfTest.groupby(['Receipt','Date']).count().reset_index()


Receipt Date    Product
0   10001   08/20/10    2
1   10002   08/20/10    1
2   10002   08/21/10    1
3   10003   08/22/10    1
4   10004   08/24/10    1
5   10004   08/25/10    1
6   10004   08/26/10    1
</code></pre>

<p>Now I'm not sure how to proceed.  I hope someone out there can lend a helping hand. This might be easy, I'm just a bit confused or lacking in experience. </p>

<p>Another solution using <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html" rel="nofollow"><code>reindex</code></a> in combination with <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#filtration" rel="nofollow"><code>groupby.filter</code></a> and filtering based on the number of unique lengths:</p>

<pre><code>df.reindex(df.groupby(['Receipt'])['Date'].filter(lambda x: len(x.unique()) == 1).index)
</code></pre>

<p><a href="http://i.stack.imgur.com/UKqMr.png" rel="nofollow"><img src="http://i.stack.imgur.com/UKqMr.png" alt="Image"></a></p>

python pandas