0

I have the dataframe as following:

(cusid means the customer id; product means product id bought by the customer; count means the purchased count of this product.)

cusid   product count
1521    30  2
18984   99  1
25094   1   1
2363    36  1
3316    21  1
19249   228 1
13220   78  1
1226    79  4
1117    112 2


I want to calculate the average number of every product that every customer would buy. Seeming need to get groupby product in cusid, then groupby product in count, then get the mean. my expect ouput:

product mean(count)
30       
99       
1        
36       

Here is my code:

(df.groupby(['product','cusid']).mean().groupby('product')['count'].mean())

got the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-43-0fac990bbd61> in <module>()
----> 1 (df.groupby(['product','cusid']).mean().groupby('product')['count'].mean())

TypeError: groupby() takes at least 3 arguments (2 given

have no idea how to fix it

Poppy Bee
  • 154
  • 2
  • 3
  • 11

1 Answers1

0
df.groupby(['cusid', 'product']).mean().reset_index().groupby('product')['count'].mean()

OUTPUT:

product
1      1
21     1
30     2
36     1
78     1
79     4
99     1
112    2
228    1

python version: 3.7.4 pandas version: 0.25.0

Wenmin Wu
  • 1,808
  • 12
  • 24