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, optional
Drop groups that do not pass the filter. True by default; if False, groups that evaluate False are filled with NaNs.
- *argstuple
Optional positional arguments to pass to func.
- **kwargsdict
Optional keyword arguments to pass to func.
- Returns:
- Series
The filtered subset of the original Series.
See also
Series.filter
Filter elements of ungrouped Series.
DataFrameGroupBy.filter
Filter elements from groups base on criterion.
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.0, 8.0, 1.0, 2.0, 9.0], ... } ... ) >>> grouped = df.groupby("A") >>> df.groupby("A").B.filter(lambda x: x.mean() > 3.0) 1 2 3 4 5 6 Name: B, dtype: int64