I am looking to understand how to filter a groupby object.
I am generating this through:
groupby = df.groupby([\'Order #\', \'ProductLine\', \'Product
filter
takes a callable that returns a boolean. That callable will take the entire groups dataframe. If the boolean is True
, the dataframe comes back. If False
then nothing comes back.
Only A
def f(df):
v = df.ProductLine.values
return (v == 'A').all()
df.groupby(['Order #', 'ProductLine', 'ProductType']).filter(f)
A
and not Z
def f(df):
v = df.ProductLine.values
return ('A' in v) and ('Z' not in v)
df.groupby(['Order #', 'ProductLine', 'ProductType']).filter(f)