9

I am trying to get sum, mean and count of a metric

df.groupby(['id', 'pushid']).agg({"sess_length": [ np.sum, np.mean, np.count]})

But I get "module 'numpy' has no attribute 'count'", and I have tried different ways of expressing the count function but can't get it to work. How do I just an aggregate record count together with the other metrics?

3
  • Do you just want len? Not sure what you mean about different ways of expressing the count function - numpy certainly doesn't have np.count, as you've seen. What is this function expected to do?
    – Nathan
    Commented Apr 9, 2019 at 18:32
  • you can use np.size
    – jxc
    Commented Apr 9, 2019 at 18:37
  • @jxc size will count nan as a row, count will exclude nan
    – YJZ
    Commented Jul 8, 2019 at 21:00

4 Answers 4

10

You can use strings instead of the functions, like so:

df = pd.DataFrame(
    {"id": list("ccdef"), "pushid": list("aabbc"), 
     "sess_length": [10, 20, 30, 40, 50]}
)

df.groupby(["id", "pushid"]).agg({"sess_length": ["sum", "mean", "count"]})

Which outputs:

           sess_length
                   sum mean count
 id pushid
 c  a               30   15     2
 d  b               30   30     1
 e  b               40   40     1
 f  c               50   50     1
3

just use np.size

Not sure why the answer needs to be 30 chars long, when the answer is straightforward

1
  • I was looking for that exactly. np.count_nonzero didn't look suitable.
    – Rafs
    Commented Aug 29, 2023 at 13:08
1

This might work:

df.groupby(['id', 'pushid']).agg({"sess_length": [ np.sum, np.mean, np.**size**]})
1
  • Is there a benefit to this syntax over the use of [ 'sum', 'mean', 'count'], as described in the accepted answer from last year? If so, it'd be useful to edit your answer to include that. Commented Oct 28, 2020 at 21:33
0

I think you mean :

df.groupby(['id', 'pushid']).agg({"sess_length": [ 'sum', 'count','mean']})

As mentioned in documentation of pandas, you can use string arguments like 'sum','count'. TBH It's more preferable way of doing these aggregations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.