When a filter is executed based on the result of the Pandas groupby operation, it returns a dataframe. But, believing that I want to do further group calculations, I need to call the group again, which seems something like this. Is there a more idiomatic way to do this?
EDIT:
To illustrate what I'm talking about:
We shamelessly steal a toy frame from Pandas documents and the group:
>>> dff = pd.DataFrame({'A': np.arange(8), 'B': list('aabbbbcc')}) >>> grouped = dff.groupby('B') >>> type(grouped) <class 'pandas.core.groupby.DataFrameGroupBy'>
Returns a groupby object over which we can iterate, perform group operations, etc. But if we filter:
>>> filtered = grouped.filter(lambda x: len(x) > 2) >>> type(filtered) <class 'pandas.core.frame.DataFrame'>
We are returning a data frame. Is there a good idiomatic way to get the filtered groups back, and not just the source strings belonging to the filtered groups?
source share