I am trying to count how many orders there was for each product, by using the line of code below
TSP_Order = TSP_Order.groupby(['ProdName','Year']).count('OrderID')
But i receive the error below TypeError: count() takes 1 positional argument but 2 were given
An example of my dataset
| ProdName | OrderID | Year |
|---|---|---|
| Toy Car | 1 | 2015 |
| Puzzle | 1 | 2015 |
| Toy Car | 2 | 2015 |
| Train Set | 2 | 2015 |
| Toy Car | 3 | 2016 |
| Puzzle | 4 | 2016 |
| Toy Car | 5 | 2016 |
| Train Set | 5 | 2016 |
I want to calculate how many times each product was ordered per year.
CodePudding user response:
You were almost there. After grouping on the fields of interest, you need to access the column of interest, here OrderID, and perform the statistic you need (simple count in this case):
df.groupby(['ProdName','Year'])['OrderID'].count()
ProdName Year
Puzzle 2015 1
2016 1
Toy Car 2015 2
2016 2
Train Set 2015 1
2016 1
Name: OrderID, dtype: int64
with
df
ProdName OrderID Year
0 Toy Car 1 2015
1 Puzzle 1 2015
2 Toy Car 2 2015
3 Train Set 2 2015
4 Toy Car 3 2016
5 Puzzle 4 2016
6 Toy Car 5 2016
7 Train Set 5 2016
