I often end up doing things like this in pandas
:
s2 = s1.groupby(level=1).sum()
s2 = s2[s2>25]
In words, I do some groupby
operation and then want to keep only results that meet some condition for the result.
Is there a way to do with in one line? More specifically, is it possible to do this without creating the series and then doing the Boolean selection in a second step?
Assuming s1
is a pandas.Series
level
to pd.Series.sum
pd.Series.compress
is handys2.sum(level=1).compress(lambda s: s.gt(25))
Assuming s1
is a pandas.DataFrame
And that there is a column names 'col'
s.sum(level=1).query('col > 25')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With