I wish to conduct operations on groups in my data frame but only upon filtered groups. It seems for me to do so I have to groupby, then filter, and then groupby again, and finally conduct my operation. Is there some means to bypass the second grouping operation? Seems wasteful if I actually want to retain the context of being a GroupBy object.
df2 = df1.groupby('ColumnX').filter( lambda x: len(x) >= 2 ).groupby('ColumnX').mean()
Is there a way to conduct a group filter whose return value is not a dataframe but a GroupBy object?
Here's some toy data to demonstrate with...
>>> df = pandas.DataFrame({ 'ColumnX' : ['a', 'a', 'a' , 'b' , 'b' , 'c' ] , 'ColumnY' : [ 1 , 2 , 3 , 1 , 2 , 99 ] })
>>> df
ColumnX ColumnY
0 a 1
1 a 2
2 a 3
3 b 1
4 b 2
5 c 99
>>> df.groupby('ColumnX')
<pandas.core.groupby.DataFrameGroupBy object at 0x0000000005974438>
>>> df.groupby('ColumnX').filter( lambda x : len(x) >=2 )
ColumnX ColumnY
0 a 1
1 a 2
2 a 3
3 b 1
4 b 2
>>> df.groupby('ColumnX').filter( lambda x : len(x) >=2 ).groupby('ColumnX')
<pandas.core.groupby.DataFrameGroupBy object at 0x00000000033A1588>
>>> df.groupby('ColumnX').filter( lambda x : len(x) >=2 ).groupby('ColumnX').mean()
ColumnY
ColumnX
a 2.0
b 1.5
>>>