15

Pandas Groupby apply function to count values greater than zero

I am using groupby and agg in the following manner:

df.groupby('group')['a'].agg({'mean' : np.mean, 'std' : np.std})

and I would like to also count the values above zero in the same column ['a']

the following line does the count as I want,

sum(x > 0 for x in df['a'])

but I can't get it work when applying to groupby.

Following an example for applying a pandas calculation to a groupby I tried:

df.groupby('group')['a'].apply(sum(x > 0 for x in df['a']))

but I get an error message: AttributeError: 'numpy.int32' object has no attribute 'module'

Can anybody please suggest how this might be done?

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
rdh9
  • 665
  • 2
  • 11
  • 20

1 Answers1

14

Answer from the comments:

 .agg({'pos':lambda ts: (ts > 0).sum()}) # –  behzad.nouri Mar 31 at 0:00 

This is my contribution to the backlog of unanswered questions :) Credits to behzad.nouri

Update 2020 In the latest pandas version, you need to do the following:

 .agg(pos=lambda ts: (ts > 0).sum()) 

otherwise it will result in the following error:

SpecificationError: nested renamer is not supported
Jeril
  • 7,858
  • 3
  • 52
  • 69
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80