in pandas I want to do:
df.groupby('A').filter(lambda x: x.name > 0)
- group by column A
and then filter groups that have the value of the name non positive. However this canceles the grouping as GroupBy.filter
returns DataFrame
and thus losing the groupings. I want to do it in this order as it should be less computationaly demanding because filter
followed by groupby
would walk the DataFrame twice no (first filtering and then grouping)? Also cloning the groups from the grouping (to a dict or something) would lose me the functionality to seamlessly go back to dataframe (like in the example of .filter
that you directly get the DataFrame
)
Thanks
Example:
A B
1 -1 1
2 -1 2
3 0 2
4 1 1
5 1 2
df.groupby('A')
:
GroupBy object
-1 : [1, 2]
0 : [3]
1 : [4,5]
GroupBy.filter(lambda x: x.name >= 0)
:
GroupBy object
0 : [3]
1 : [4,5]