pandas.core.groupby.SeriesGroupBy.filter#
- SeriesGroupBy.filter(func, dropna=True, *args, **kwargs)[source]#
Filter elements from groups that don’t satisfy a criterion.
Elements from groups are filtered if they do not satisfy the boolean criterion specified by func.
- Parameters:
- funcfunction
Criterion to apply to each group. Should return True or False.
- dropnabool
Drop groups that do not pass the filter. True by default; if False, groups that evaluate False are filled with NaNs.
- Returns:
- Series
Notes
Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See Mutating with User Defined Function (UDF) methods for more details.
Examples
>>> df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', ... 'foo', 'bar'], ... 'B' : [1, 2, 3, 4, 5, 6], ... 'C' : [2.0, 5., 8., 1., 2., 9.]}) >>> grouped = df.groupby('A') >>> df.groupby('A').B.filter(lambda x: x.mean() > 3.) 1 2 3 4 5 6 Name: B, dtype: int64