DataFrameGroupBy.filter(self, func, dropna=True, *args, **kwargs)
[source]
Return a copy of a DataFrame excluding elements from groups that do not satisfy the boolean criterion specified by func.
Parameters: |
|
---|---|
Returns: |
|
Each subframe is endowed the attribute ‘name’ in case you need to know which group you are working on.
>>> 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') >>> grouped.filter(lambda x: x['B'].mean() > 3.) A B C 1 bar 2 5.0 3 bar 4 1.0 5 bar 6 9.0
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.core.groupby.DataFrameGroupBy.filter.html