pandas groupby and boolean selection

Viewed 6700

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?

2 Answers

Assuming s1 is a pandas.Series

  1. You can pass level to pd.Series.sum
  2. pd.Series.compress is handy

s2.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')

.loc + lambda

Serise

df.groupby('name').a.sum().loc[lambda x: x>20]
Out[492]: 
name
Chn     84
Fra    165
Ind     71

Dataframe

df.groupby('name').a.sum().to_frame().loc[lambda x: x['a']>20]
Out[490]: 
        a
name     
Chn    84
Fra   165
Ind    71

DataInput

df=pd.DataFrame({'name':['Ind','Chn','SG','US','SG','US','Ind','Chn','Fra','Fra'],'a':[5,6,3,4,7,12,66,78,65,100]})
Related