Filtering a Pandas Dataframe by an aggregate function?

Question:

So I have dataframe that looks like this:

    STORE    PRODUCT    INVENTORY
1   store1      a           1
2   store1      b           0
3   store2      a           0
4   store2      b           0
5   store3      a           1
6   store3      b           1

I want to filter this such that it only shows me stores with a total inventory > 0 in the sense that the result will be as follows because store2 has a total inventory of 0.

    STORE    PRODUCT    INVENTORY
1   store1      a           1
2   store1      b           0
5   store3      a           1
6   store3      b           1

How do I do this?

Asked By: Ari Frid

||

Answers:

You can try:

df.loc[(df.groupby('STORE')['INVENTORY'].transform(sum) > 0)]

    STORE PRODUCT  INVENTORY
1  store1       a          1
2  store1       b          0
5  store3       a          1
6  store3       b          1
Answered By: Anoushiravan R

We can use filter like below

df.groupby('STORE').filter(lambda x: x['INVENTORY'].sum()>0)

which gives

    STORE   PRODUCT INVENTORY
1   store1  a   1
2   store1  b   0
5   store3  a   1
6   store3  b   1

Data

import pandas as pd
import io

text = """
    STORE    PRODUCT    INVENTORY
1   store1      a           1
2   store1      b           0
3   store2      a           0
4   store2      b           0
5   store3      a           1
6   store3      b           1
"""
df = pd.read_csv(io.StringIO(text), sep = 's+')
Answered By: ThomasIsCoding
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.